Professional C__ - Marc Gregoire [233]
/usr/include/c++/3.2.2/bits/stl_algo.h: In function 'void
std::sort(_RandomAccessIter, _RandomAccessIter) [with _RandomAccessIter =
std::_List_iterator Sorting.cpp:38: instantiated from here /usr/include/c++/3.2.2/bits/stl_algo.h:2178: no match for ' std::_List_iterator Most of the algorithms are defined in the Utility Algorithms The STL provides four utility algorithms implemented as function templates: min(), max(), swap() and the C++11 minmax(). The min() and max() functions compare two elements of any type using operator< or a user-supplied binary predicate, returning a const reference to the smaller or larger element, respectively. The swap() function takes two elements of any type by reference and switches their values. With C++11, the min() and max() algorithms can be used to compare more than two values, while the minmax() algorithm returns a pair containing the minimum and the maximum value of two or more elements. These utilities do not work on sequences of elements, so they do not take iterator parameters. The following program demonstrates the four algorithms: int x = 4, y = 5; cout << "x is " << x << " and y is " << y << endl; cout << "Max is " << max(x, y) << endl; cout << "Min is " << min(x, y) << endl; swap(x, y); cout << "x is " << x << " and y is " << y << endl; cout << "Max is " << max(x, y) << endl; cout << "Min is " << min(x, y) << endl; // C++11: using max and min on more than two values int x1 = 2, x2 = 9, x3 = 3, x4 = 12; cout << "Max of 4 elements is " << max({x1,x2,x3,x4}) << endl; cout << "Min of 4 elements is " << min({x1,x2,x3,x4}) << endl; // C++11: using minmax auto p2 = minmax({x1,x2,x3,x4}); cout << "Minmax of 4 elements is <" << p2.first << "," << p2.second << ">" << endl; Code snippet from UtilityAlgorithms\utilities.cpp Here is the program output: x is 4 and y is 5 Max is 5 Min is 4 x is 5 and y is 4 Max is 5 Min is 4 Max of 4 elements is 12 Min of 4 elements is 2 Minmax of 4 elements is <2,12> The C language also includes a min() and max() function; however, they are implemented as macros, and will potentially evaluate one of their arguments twice; whereas std::min() and std::max() evaluate each argument exactly once. Make sure you always use the C++ versions, std::min() and std::max(), either by explicitly specifying std:: or by using a using namespace std clause. Non-Modifying Algorithms The non-modifying algorithms include functions for searching elements in a range, generating numerical information about elements in a range, comparing two ranges to each other, and processing each element in a range. Search Algorithms You’ve already seen three examples of using search algorithms: find(), find_if(), and the C++11 find_if_not(). The STL provides several other variations of the basic find() algorithm that work on sequences of elements. The section “Search Algorithms” in Chapter 11 describes the different search algorithms that are available, including their complexity. All the algorithms use default comparisons of operator== or operator<, but also provide overloaded versions that allow you to specify a comparison callback. Here are examples of some of the search algorithms: // The list of elements to be searched vector auto begin = myVector.begin(); auto end = myVector.end(); // Find the min and max elements in the vector auto it = min_element(begin, end); auto it2 = max_element(begin, end); cout << "min is " << *it << " and max is " << *it2 << endl; // Find the first pair of matching consecutive elements it = adjacent_find(begin, end); if (it != end) { cout << "Found two consecutive equal elements with value " << *it << endl; } // Find the first of two values vector