Professional C__ - Marc Gregoire [234]
it = find_first_of(begin, end, targets.begin(), targets.end());
if (it != end) {
cout << "Found one of 8 or 9: " << *it << endl;
}
// Find the first subsequence
vector it = search(begin, end, sub.begin(), sub.end()); if (it != end) { cout << "Found subsequence {8,3}" << endl; } else { cout << "Unable to find subsequence {8,3}" << endl; } // Find the last subsequence (which is the same as the first in this example) it2 = find_end(begin, end, sub.begin(), sub.end()); if (it != it2) { cout << "Error: search and find_end found different subsequences " << "even though there is only one match." << endl; } // Find the first subsequence of two consecutive 8s it = search_n(begin, end, 2, 8); if (it != end) { cout << "Found two consecutive 8s" << endl; } else { cout << "Unable to find two consecutive 8s" << endl; } Code snippet from NonModifyingAlgorithms\SearchAlgorithms.cpp Here is the output: min is 3 and max is 9 Found two consecutive equal elements with value 8 Found one of 8 or 9: 9 Found subsequence {8,3} Found two consecutive 8s There are also several search algorithms that work only on sorted sequences: binary_search(), lower_bound(), upper_bound(), and equal_range(). Examples of sorted sequences are vectors whose contents are sorted, map, multimap, set, and multiset. The binary_search() algorithm finds a matching element in logarithmic time (see Chapter 2). The other three are similar to their method equivalents on the map and set containers. See Chapter 12 for an example on how to use them. Remember to use equivalent container methods when available instead of generic algorithms, because the methods are more efficient. The section “Search Algorithms” in Chapter 11 also describes a number of new C++11 search algorithms: find_if_not(), minmax_element(), all_of(), any_of(), none_of(), and partition_point(). The following example shows these new algorithms in action. This example also demonstrates the use of cbegin() and cend() to get const iterators. vector auto begin = vec.cbegin(); auto end = vec.cend(); // Find an element != 0 auto it = find_if_not(begin, end, [](int i){return i == 0;}); if (it == end) cout << "No element found != 0" << endl; else cout << "Found element " << *it << " != 0" << endl; // Find min and max with 1 algorithm auto minmax = minmax_element(begin, end); cout << "Min = " << *(minmax.first) << " and Max = " << *(minmax.second) << endl; // all_of() vector if (all_of(vec2.cbegin(), vec2.cend(), [](int i){return i == 1;})) cout << "All elements are == 1" << endl; else cout << "Not all elements are == 1" << endl; // any_of() vector if (any_of(vec3.cbegin(), vec3.cend(), [](int i){return i == 1;})) cout << "At least one element == 1" << endl; else cout << "No elements are == 1" << endl; // none_of() vector if (none_of(vec4.cbegin(), vec4.cend(), [](int i){return i == 1;})) cout << "All elements are != 1" << endl; else cout << "Some elements are == 1" << endl; // partition_point() vector auto ppoint = partition_point(vec5.cbegin(), vec5.cend(), [](int i){return i == 1;}); cout << "Partition point at position " << (ppoint-vec5.cbegin()) << endl; Code snippet from NonModifyingAlgorithms\Cpp11SearchAlgorithms.cpp The output is as follows: Found element 1 != 0 Min = 0 and Max = 2 All elements are == 1 At least one element == 1 All elements are != 1 Partition point at position 2 Numerical Processing Algorithms You’ve seen an example of one numerical processing algorithm already: accumulate(). In addition, the count() and count_if() algorithms are useful for counting the number of elements of a given value in a container. They function similarly to the count() method on the map and set containers. See section “Numerical Processing Algorithms” in Chapter 11 for a description of all numerical processing algorithms that are available. As another example, the following