c++ algorithm 算法库

本贴最后更新于 303 天前,其中的信息可能已经天翻地覆

第一部分:非修改序列操作

1:all_of

/* 返回迭代范围内所有符合条件的元素,如果pred为范围[first,last]中的所有元素返回true,或者如果范围为空,则返回true,否则返回false template <class InputIterator, class UnaryPredicate> bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred); */ // all_of example #include <iostream> // std::cout #include <algorithm> // std::all_of #include <array> // std::array int main () { std::array<int,8> foo = {3,5,7,11,13,17,19,23}; if ( std::all_of(foo.begin(), foo.end(), [](int i){return i%2;}) ) std::cout << "All the elements are odd numbers.\n"; return 0; } Output: All the elements are odd numbers.

2:any_of

/* 测试迭代范围内的任何元素是否满足条件,如果pred对范围[first,last)中的任何元素返回true,则返回true,否则返回false。 template <class InputIterator, class UnaryPredicate> bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred); */ // any_of example #include <iostream> // std::cout #include <algorithm> // std::any_of #include <array> // std::array int main () { std::array<int,7> foo = {0,1,-1,3,-3,5,-5}; if ( std::any_of(foo.begin(), foo.end(), [](int i){return i<0;}) ) std::cout << "There are negative elements in the range.\n"; return 0; } Output: There are negative elements in the range.

3:none_of

/* 测试是否没有元素满足条件 如果pred对范围[first,last)中的所有元素返回false,或者如果范围为空,则返回true,否则返回false。 template <class InputIterator, class UnaryPredicate> bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred); */ // none_of example #include <iostream> // std::cout #include <algorithm> // std::none_of #include <array> // std::array int main () { std::array<int,8> foo = {1,2,4,8,16,32,64,128}; if ( std::none_of(foo.begin(), foo.end(), [](int i){return i<0;}) ) std::cout << "There are no negative elements in the range.\n"; return 0; } Output: There are no negative elements in the range.

4:for_each

/* 将函数应用于范围 template <class InputIterator, class Function> Function for_each (InputIterator first, InputIterator last, Function fn); */ // for_each example #include <iostream> // std::cout #include <algorithm> // std::for_each #include <vector> // std::vector void myfunction (int i) { // function: std::cout << ' ' << i; } struct myclass { // function object type: void operator() (int i) {std::cout << ' ' << i;} } myobject; int main () { std::vector<int> myvector; myvector.push_back(10); myvector.push_back(20); myvector.push_back(30); std::cout << "myvector contains:"; for_each (myvector.begin(), myvector.end(), myfunction); std::cout << '\n'; // or: std::cout << "myvector contains:"; for_each (myvector.begin(), myvector.end(), myobject); std::cout << '\n'; return 0; } Output: myvector contains: 10 20 30 myvector contains: 10 20 30

5:find

/* 查找范围内的值 将迭代器返回到范围[first,last)中第一个比较等于val的元素。如果找不到这样的元素,函数将返回last。 template <class InputIterator, class Function> Function for_each (InputIterator first, InputIterator last, Function fn); */ // find example #include <iostream> // std::cout #include <algorithm> // std::find #include <vector> // std::vector int main () { // using std::find with array and pointer: int myints[] = { 10, 20, 30, 40 }; int * p; p = std::find (myints, myints+4, 30); if (p != myints+4) std::cout << "Element found in myints: " << *p << '\n'; else std::cout << "Element not found in myints\n"; // using std::find with vector and iterator: std::vector<int> myvector (myints,myints+4); std::vector<int>::iterator it; it = find (myvector.begin(), myvector.end(), 30); if (it != myvector.end()) std::cout << "Element found in myvector: " << *it << '\n'; else std::cout << "Element not found in myvector\n"; return 0; } Output: Element found in myints: 30 Element found in myvector: 30

6:find_if

/* 查找范围中的元素 为pred返回true的范围[first,last)中的第一个元素返回迭代器。如果找不到这样的元素,函数将返回last。 template <class InputIterator, class Function> Function for_each (InputIterator first, InputIterator last, Function fn); */ // find_if example #include <iostream> // std::cout #include <algorithm> // std::find_if #include <vector> // std::vector bool IsOdd (int i) { return ((i%2)==1); } int main () { std::vector<int> myvector; myvector.push_back(10); myvector.push_back(25); myvector.push_back(40); myvector.push_back(55); std::vector<int>::iterator it = std::find_if (myvector.begin(), myvector.end(), IsOdd); std::cout << "The first odd value is " << *it << '\n'; return 0; } Output: The first odd value is 25

7:find_if_not

/* 查找范围内的元素(负条件)为pred返回false的范围[first,last)中的第一个元素返回迭代器。如果找不到这样的元素,函数将返回last。 template <class InputIterator, class UnaryPredicate> InputIterator find_if_not (InputIterator first, InputIterator last, UnaryPredicate pred); */ // find_if_not example #include <iostream> // std::cout #include <algorithm> // std::find_if_not #include <array> // std::array int main () { std::array<int,5> foo = {1,2,3,4,5}; std::array<int,5>::iterator it = std::find_if_not (foo.begin(), foo.end(), [](int i){return i%2;} ); std::cout << "The first even value is " << *it << '\n'; return 0; } Output: The first even value is 2

8:find_end

/* 查找范围中的最后一个子序列 在[first1,last1)中最后一次出现[first2,last2)的第一个元素的迭代器。如果没有找到序列,函数将返回last1。 equality (1) template <class ForwardIterator1, class ForwardIterator2> ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); predicate (2) template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); */ // find_end example #include <iostream> // std::cout #include <algorithm> // std::find_end #include <vector> // std::vector bool myfunction (int i, int j) { return (i==j); } int main () { int myints[] = {1,2,3,4,5,1,2,3,4,5}; std::vector<int> haystack (myints,myints+10); int needle1[] = {1,2,3}; // using default comparison: std::vector<int>::iterator it; it = std::find_end (haystack.begin(), haystack.end(), needle1, needle1+3); if (it!=haystack.end()) std::cout << "needle1 last found at position " << (it-haystack.begin()) << '\n'; int needle2[] = {4,5,1}; // using predicate comparison: it = std::find_end (haystack.begin(), haystack.end(), needle2, needle2+3, myfunction); if (it!=haystack.end()) std::cout << "needle2 last found at position " << (it-haystack.begin()) << '\n'; return 0; } Output: needle1 last found at position 5 needle2 last found at position 3

8:find_first_of

/* 从范围中的集合中查找元素 将迭代器返回到范围[first1,last1)中的第一个元素,该元素与[first2,last2)中的任何元素匹配。如果找不到这样的元素,函数将返回last1。 equality (1) template <class InputIterator, class ForwardIterator> InputIterator find_first_of (InputIterator first1, InputIterator last1, ForwardIterator first2, ForwardIterator last2); predicate (2) template <class InputIterator, class ForwardIterator, class BinaryPredicate> InputIterator find_first_of (InputIterator first1, InputIterator last1, ForwardIterator first2, ForwardIterator last2, BinaryPredicate pred); */ // find_first_of example #include <iostream> // std::cout #include <algorithm> // std::find_first_of #include <vector> // std::vector #include <cctype> // std::tolower bool comp_case_insensitive (char c1, char c2) { return (std::tolower(c1)==std::tolower(c2)); } int main () { int mychars[] = {'a','b','c','A','B','C'}; std::vector<char> haystack (mychars,mychars+6); std::vector<char>::iterator it; int needle[] = {'A','B','C'}; // using default comparison: it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3); if (it!=haystack.end()) std::cout << "The first match is: " << *it << '\n'; // using predicate comparison: it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3, comp_case_insensitive); if (it!=haystack.end()) std::cout << "The first match is: " << *it << '\n'; return 0; } Output: The first match is: A The first match is: a

9:adjacent_find

/* 查找范围内相等的相邻元素 在范围[first,last)中搜索匹配的两个连续元素的第一次出现,并将迭代器返回到这两个元素中的第一个,如果找不到这样的对,则返回到最后一个。 equality (1) template <class ForwardIterator> ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last); predicate (2) template <class ForwardIterator, class BinaryPredicate> ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last, BinaryPredicate pred); */ // adjacent_find example #include <iostream> // std::cout #include <algorithm> // std::adjacent_find #include <vector> // std::vector bool myfunction (int i, int j) { return (i==j); } int main () { int myints[] = {5,20,5,30,30,20,10,10,20}; std::vector<int> myvector (myints,myints+8); std::vector<int>::iterator it; // using default comparison: it = std::adjacent_find (myvector.begin(), myvector.end()); if (it!=myvector.end()) std::cout << "the first pair of repeated elements are: " << *it << '\n'; //using predicate comparison: it = std::adjacent_find (++it, myvector.end(), myfunction); if (it!=myvector.end()) std::cout << "the second pair of repeated elements are: " << *it << '\n'; return 0; } Output: the first pair of repeated elements are: 30 the second pair of repeated elements are: 10

10:count

/* 计数范围内值的出现次数 返回范围[第一个,最后一个]中与值比较相等的元素数。 template <class InputIterator, class T> typename iterator_traits<InputIterator>::difference_type count (InputIterator first, InputIterator last, const T& val); */ // count algorithm example #include <iostream> // std::cout #include <algorithm> // std::count #include <vector> // std::vector int main () { // counting elements in array: int myints[] = {10,20,30,30,20,10,10,20}; // 8 elements int mycount = std::count (myints, myints+8, 10); std::cout << "10 appears " << mycount << " times.\n"; // counting elements in container: std::vector<int> myvector (myints, myints+8); mycount = std::count (myvector.begin(), myvector.end(), 20); std::cout << "20 appears " << mycount << " times.\n"; return 0; } Output: 10 appears 3 times. 20 appears 3 times.

11:count_if

/* 返回满足条件的范围内的元素数 返回pred为true的范围[first,last]中的元素数。 template <class InputIterator, class UnaryPredicate> typename iterator_traits<InputIterator>::difference_type count_if (InputIterator first, InputIterator last, UnaryPredicate pred); */ // count_if example #include <iostream> // std::cout #include <algorithm> // std::count_if #include <vector> // std::vector bool IsOdd (int i) { return ((i%2)==1); } int main () { std::vector<int> myvector; for (int i=1; i<10; i++) myvector.push_back(i); // myvector: 1 2 3 4 5 6 7 8 9 int mycount = count_if (myvector.begin(), myvector.end(), IsOdd); std::cout << "myvector contains " << mycount << " odd values.\n"; return 0; } Output: myvector contains 5 odd values.

12:mismatch

/* 返回两个范围不同的第一个位置 将范围[first1,last1)中的元素与从first2开始的范围中的元素进行比较,并返回两个序列中第一个不匹配的元素。 equality (1) template <class InputIterator1, class InputIterator2> pair<InputIterator1, InputIterator2> mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2); predicate (2) template <class InputIterator1, class InputIterator2, class BinaryPredicate> pair<InputIterator1, InputIterator2> mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred); */ // mismatch algorithm example #include <iostream> // std::cout #include <algorithm> // std::mismatch #include <vector> // std::vector #include <utility> // std::pair bool mypredicate (int i, int j) { return (i==j); } int main () { std::vector<int> myvector; for (int i=1; i<6; i++) myvector.push_back (i*10); // myvector: 10 20 30 40 50 int myints[] = {10,20,80,320,1024}; // myints: 10 20 80 320 1024 std::pair<std::vector<int>::iterator,int*> mypair; // using default comparison: mypair = std::mismatch (myvector.begin(), myvector.end(), myints); std::cout << "First mismatching elements: " << *mypair.first; std::cout << " and " << *mypair.second << '\n'; ++mypair.first; ++mypair.second; // using predicate comparison: mypair = std::mismatch (mypair.first, myvector.end(), mypair.second, mypredicate); std::cout << "Second mismatching elements: " << *mypair.first; std::cout << " and " << *mypair.second << '\n'; return 0; } Output: First mismatching elements: 30 and 80 Second mismatching elements: 40 and 320

13:equal

/* 测试两个范围内的元素是否相等 将范围[first1,last1)中的元素与从first2开始的范围中的元素进行比较,如果两个范围中的所有元素都匹配,则返回true。 equality (1) template <class InputIterator1, class InputIterator2> bool equal (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2); predicate (2) template <class InputIterator1, class InputIterator2, class BinaryPredicate> bool equal (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred); */ // equal algorithm example #include <iostream> // std::cout #include <algorithm> // std::equal #include <vector> // std::vector bool mypredicate (int i, int j) { return (i==j); } int main () { int myints[] = {20,40,60,80,100}; // myints: 20 40 60 80 100 std::vector<int>myvector (myints,myints+5); // myvector: 20 40 60 80 100 // using default comparison: if ( std::equal (myvector.begin(), myvector.end(), myints) ) std::cout << "The contents of both sequences are equal.\n"; else std::cout << "The contents of both sequences differ.\n"; myvector[3]=81; // myvector: 20 40 60 81 100 // using predicate comparison: if ( std::equal (myvector.begin(), myvector.end(), myints, mypredicate) ) std::cout << "The contents of both sequences are equal.\n"; else std::cout << "The contents of both sequences differ.\n"; return 0; } Output: The contents of both sequences are equal. The contents of both sequence differ.

14:is_permutation

/* 测试范围是否为另一个的排列 将范围[first1,last1)中的元素与从first2开始的范围中的元素进行比较,如果两个范围中的所有元素都匹配(即使顺序不同),则返回true。 equality (1) template <class ForwardIterator1, class ForwardIterator2> bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2); predicate (2) template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, BinaryPredicate pred); */ // is_permutation example #include <iostream> // std::cout #include <algorithm> // std::is_permutation #include <array> // std::array int main () { std::array<int,5> foo = {1,2,3,4,5}; std::array<int,5> bar = {3,1,4,5,2}; if ( std::is_permutation (foo.begin(), foo.end(), bar.begin()) ) std::cout << "foo and bar contain the same elements.\n"; return 0; } Output: foo and bar contain the same elements.
/* 子序列的搜索范围 在范围[first1,last1)中搜索[first2,last2)定义的序列的第一个出现项,并将迭代器返回到其第一个元素,如果未找到出现项,则返回last1。 equality (1) template <class ForwardIterator1, class ForwardIterator2> ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); predicate (2) template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); */ // search algorithm example #include <iostream> // std::cout #include <algorithm> // std::search #include <vector> // std::vector bool mypredicate (int i, int j) { return (i==j); } int main () { std::vector<int> haystack; // set some values: haystack: 10 20 30 40 50 60 70 80 90 for (int i=1; i<10; i++) haystack.push_back(i*10); // using default comparison: int needle1[] = {40,50,60,70}; std::vector<int>::iterator it; it = std::search (haystack.begin(), haystack.end(), needle1, needle1+4); if (it!=haystack.end()) std::cout << "needle1 found at position " << (it-haystack.begin()) << '\n'; else std::cout << "needle1 not found\n"; // using predicate comparison: int needle2[] = {20,30,50}; it = std::search (haystack.begin(), haystack.end(), needle2, needle2+3, mypredicate); if (it!=haystack.end()) std::cout << "needle2 found at position " << (it-haystack.begin()) << '\n'; else std::cout << "needle2 not found\n"; return 0; } Output: needle1 found at position 3 needle2 not found

16:search_n

/* 元素的搜索范围 在范围[第一个,最后一个)中搜索计数元素序列,每个元素的比较值等于val(或pred返回true)。 equality (1) template <class ForwardIterator, class Size, class T> ForwardIterator search_n (ForwardIterator first, ForwardIterator last, Size count, const T& val); predicate (2) template <class ForwardIterator, class Size, class T, class BinaryPredicate> ForwardIterator search_n ( ForwardIterator first, ForwardIterator last, Size count, const T& val, BinaryPredicate pred ); */ // search_n example #include <iostream> // std::cout #include <algorithm> // std::search_n #include <vector> // std::vector bool mypredicate (int i, int j) { return (i==j); } int main () { int myints[]={10,20,30,30,20,10,10,20}; std::vector<int> myvector (myints,myints+8); std::vector<int>::iterator it; // using default comparison: it = std::search_n (myvector.begin(), myvector.end(), 2, 30); if (it!=myvector.end()) std::cout << "two 30s found at position " << (it-myvector.begin()) << '\n'; else std::cout << "match not found\n"; // using predicate comparison: it = std::search_n (myvector.begin(), myvector.end(), 2, 10, mypredicate); if (it!=myvector.end()) std::cout << "two 10s found at position " << int(it-myvector.begin()) << '\n'; else std::cout << "match not found\n"; return 0; } Output: Two 30s found at position 2<br>Two 10s found at position 5

第二部分:修改序列操作

1: copy

/* 复制元素范围 将范围[第一个,最后一个)中的元素复制到从结果开始的范围中。 template <class InputIterator, class OutputIterator> OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result); */ // copy algorithm example #include <iostream> // std::cout #include <algorithm> // std::copy #include <vector> // std::vector int main () { int myints[]={10,20,30,40,50,60,70}; std::vector<int> myvector (7); std::copy ( myints, myints+7, myvector.begin() ); std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 10 20 30 40 50 60 70

2:copy_n

/* 将从第一个开始的范围中的前n个元素复制到从结果开始的范围。 template <class InputIterator, class Size, class OutputIterator> OutputIterator copy_n (InputIterator first, Size n, OutputIterator result); */ // copy_n algorithm example #include <iostream> // std::cout #include <algorithm> // std::copy #include <vector> // std::vector int main () { int myints[]={10,20,30,40,50,60,70}; std::vector<int> myvector; myvector.resize(7); // allocate space for 7 elements std::copy_n ( myints, 7, myvector.begin() ); std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 10 20 30 40 50 60 70

3:copy_if

/* 复制范围的某些元素 将pred返回true的范围[first,last)中的元素复制到从结果开始的范围。 */ // copy_if example #include <iostream> // std::cout #include <algorithm> // std::copy_if, std::distance #include <vector> // std::vector int main () { std::vector<int> foo = {25,15,5,-5,-15}; std::vector<int> bar (foo.size()); // copy only positive numbers: auto it = std::copy_if (foo.begin(), foo.end(), bar.begin(), [](int i){return !(i<0);} ); bar.resize(std::distance(bar.begin(),it)); // shrink container to new size std::cout << "bar contains:"; for (int& x: bar) std::cout << ' ' << x; std::cout << '\n'; return 0; } Output: bar contains: 25 15 5

4:copy_backward

/* 向后复制元素范围 将范围[第一个,最后一个)中从末尾开始的元素复制到以结果结束的范围中。 template <class BidirectionalIterator1, class BidirectionalIterator2> BidirectionalIterator2 copy_backward (BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result); */ // copy_backward example #include <iostream> // std::cout #include <algorithm> // std::copy_backward #include <vector> // std::vector int main () { std::vector<int> myvector; // set some values: for (int i=1; i<=5; i++) myvector.push_back(i*10); // myvector: 10 20 30 40 50 myvector.resize(myvector.size()+3); // allocate space for 3 more elements std::copy_backward ( myvector.begin(), myvector.begin()+5, myvector.end() ); std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 10 20 30 10 20 30 40 50

5:move

/* 将范围[第一个,最后一个)中的元素移动到从结果开始的范围中。 template <class InputIterator, class OutputIterator> OutputIterator move (InputIterator first, InputIterator last, OutputIterator result); */ // move algorithm example #include <iostream> // std::cout #include <algorithm> // std::move (ranges) #include <utility> // std::move (objects) #include <vector> // std::vector #include <string> // std::string int main () { std::vector<std::string> foo = {"air","water","fire","earth"}; std::vector<std::string> bar (4); // moving ranges: std::cout << "Moving ranges...\n"; std::move ( foo.begin(), foo.begin()+4, bar.begin() ); std::cout << "foo contains " << foo.size() << " elements:"; std::cout << " (each in an unspecified but valid state)"; std::cout << '\n'; std::cout << "bar contains " << bar.size() << " elements:"; for (std::string& x: bar) std::cout << " [" << x << "]"; std::cout << '\n'; // moving container: std::cout << "Moving container...\n"; foo = std::move (bar); std::cout << "foo contains " << foo.size() << " elements:"; for (std::string& x: foo) std::cout << " [" << x << "]"; std::cout << '\n'; std::cout << "bar is in an unspecified but valid state"; std::cout << '\n'; return 0; } Possible output: Moving ranges... foo contains 4 elements: (each in an unspecified but valid state) bar contains 4 elements: [air] [water] [fire] [earth] Moving container... foo contains 4 elements: [air] [water] [fire] [earth] bar is in an unspecified but valid state

6:move_backward

/* 将范围[第一个,最后一个)中的元素从末尾开始移动到以结果结束的范围中。 template <class BidirectionalIterator1, class BidirectionalIterator2> BidirectionalIterator2 move_backward (BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result); */ // move_backward example #include <iostream> // std::cout #include <algorithm> // std::move_backward #include <string> // std::string int main () { std::string elems[10] = {"air","water","fire","earth"}; // insert new element at the beginning: std::move_backward (elems,elems+4,elems+5); elems[0]="ether"; std::cout << "elems contains:"; for (int i=0; i<10; ++i) std::cout << " [" << elems[i] << "]"; std::cout << '\n'; return 0; } Output: elems contains: [ether] [air] [water] [fire] [earth] [] [] [] [] []

7:swap

/* 交换a和b的值。 non-array (1) template <class T> void swap (T& a, T& b) noexcept (is_nothrow_move_constructible<T>::value && is_nothrow_move_assignable<T>::value); array (2) template <class T, size_t N> void swap(T (&a)[N], T (&b)[N]) noexcept (noexcept(swap(*a,*b))); */ // swap algorithm example (C++98) #include <iostream> // std::cout #include <algorithm> // std::swap #include <vector> // std::vector int main () { int x=10, y=20; // x:10 y:20 std::swap(x,y); // x:20 y:10 std::vector<int> foo (4,x), bar (6,y); // foo:4x20 bar:6x10 std::swap(foo,bar); // foo:6x10 bar:4x20 std::cout << "foo contains:"; for (std::vector<int>::iterator it=foo.begin(); it!=foo.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: foo contains: 10 10 10 10 10 10

8:swap_ranges

/* 两个范围的交换值 将范围[first1,last1)中的每个元素的值与从first2开始的范围中的相应元素的值进行交换。 template <class ForwardIterator1, class ForwardIterator2> ForwardIterator2 swap_ranges (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2); */ // swap_ranges example #include <iostream> // std::cout #include <algorithm> // std::swap_ranges #include <vector> // std::vector int main () { std::vector<int> foo (5,10); // foo: 10 10 10 10 10 std::vector<int> bar (5,33); // bar: 33 33 33 33 33 std::swap_ranges(foo.begin()+1, foo.end()-1, bar.begin()); // print out results of swap: std::cout << "foo contains:"; for (std::vector<int>::iterator it=foo.begin(); it!=foo.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; std::cout << "bar contains:"; for (std::vector<int>::iterator it=bar.begin(); it!=bar.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: foo contains: 10 33 33 33 10 bar contains: 10 10 10 33 33

9:iter_swap

/* 交换两个迭代器指向的对象的值 template <class ForwardIterator1, class ForwardIterator2> void iter_swap (ForwardIterator1 a, ForwardIterator2 b); */ // iter_swap example #include <iostream> // std::cout #include <algorithm> // std::iter_swap #include <vector> // std::vector int main () { int myints[]={10,20,30,40,50 }; // myints: 10 20 30 40 50 std::vector<int> myvector (4,99); // myvector: 99 99 99 99 std::iter_swap(myints,myvector.begin()); // myints: [99] 20 30 40 50 // myvector: [10] 99 99 99 std::iter_swap(myints+3,myvector.begin()+2); // myints: 99 20 30 [99] 50 // myvector: 10 99 [40] 99 std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 10 99 40 99

10:transform

/* 变换范围 将运算顺序应用于一(1)个或两(2)个范围的元素,并将结果存储在从结果开始的范围中。 unary operation(1) template <class InputIterator, class OutputIterator, class UnaryOperation> OutputIterator transform (InputIterator first1, InputIterator last1, OutputIterator result, UnaryOperation op); binary operation(2) template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation> OutputIterator transform (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryOperation binary_op); */ // transform algorithm example #include <iostream> // std::cout #include <algorithm> // std::transform #include <vector> // std::vector #include <functional> // std::plus int op_increase (int i) { return ++i; } int main () { std::vector<int> foo; std::vector<int> bar; // set some values: for (int i=1; i<6; i++) foo.push_back (i*10); // foo: 10 20 30 40 50 bar.resize(foo.size()); // allocate space std::transform (foo.begin(), foo.end(), bar.begin(), op_increase); // bar: 11 21 31 41 51 // std::plus adds together its two arguments: std::transform (foo.begin(), foo.end(), bar.begin(), foo.begin(), std::plus<int>()); // foo: 21 41 61 81 101 std::cout << "foo contains:"; for (std::vector<int>::iterator it=foo.begin(); it!=foo.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: foo contains: 21 41 61 81 101

11:replace

/* 替换范围内的值 将new_value分配给范围[第一个,最后一个)中与old_value比较相等的所有元素。 template <class ForwardIterator, class T> void replace (ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value); */ // replace algorithm example #include <iostream> // std::cout #include <algorithm> // std::replace #include <vector> // std::vector int main () { int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 }; std::vector<int> myvector (myints, myints+8); // 10 20 30 30 20 10 10 20 std::replace (myvector.begin(), myvector.end(), 20, 99); // 10 99 30 30 99 10 10 99 std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 10 99 30 30 99 10 10 99

12:replace_if

/* 替换范围内的值 将new_value分配给pred返回true的范围[first,last]中的所有元素。 template <class ForwardIterator, class UnaryPredicate, class T> void replace_if (ForwardIterator first, ForwardIterator last, UnaryPredicate pred, const T& new_value ); */ // replace_if example #include <iostream> // std::cout #include <algorithm> // std::replace_if #include <vector> // std::vector bool IsOdd (int i) { return ((i%2)==1); } int main () { std::vector<int> myvector; // set some values: for (int i=1; i<10; i++) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 std::replace_if (myvector.begin(), myvector.end(), IsOdd, 0); // 0 2 0 4 0 6 0 8 0 std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 0 2 0 4 0 6 0 8 0

13:replace_copy

/* 复制范围替换值 将范围[第一个,最后一个)中的元素复制到从result开始的范围,用new_value替换old_value的外观。 template <class InputIterator, class OutputIterator, class T> OutputIterator replace_copy (InputIterator first, InputIterator last, OutputIterator result, const T& old_value, const T& new_value); */ // replace_copy example #include <iostream> // std::cout #include <algorithm> // std::replace_copy #include <vector> // std::vector int main () { int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 }; std::vector<int> myvector (8); std::replace_copy (myints, myints+8, myvector.begin(), 20, 99); std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 10 99 30 30 99 10 10 99

14:replace_copy_if

/* 复制范围替换值 将范围[first,last)中的元素复制到从result开始的范围,将pred返回true的元素替换为new_value。 template <class InputIterator, class OutputIterator, class UnaryPredicate, class T> OutputIterator replace_copy_if (InputIterator first, InputIterator last, OutputIterator result, UnaryPredicate pred, const T& new_value); */ // replace_copy_if example #include <iostream> // std::cout #include <algorithm> // std::replace_copy_if #include <vector> // std::vector bool IsOdd (int i) { return ((i%2)==1); } int main () { std::vector<int> foo,bar; // set some values: for (int i=1; i<10; i++) foo.push_back(i); // 1 2 3 4 5 6 7 8 9 bar.resize(foo.size()); // allocate space std::replace_copy_if (foo.begin(), foo.end(), bar.begin(), IsOdd, 0); // 0 2 0 4 0 6 0 8 0 std::cout << "bar contains:"; for (std::vector<int>::iterator it=bar.begin(); it!=bar.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: second contains: 0 2 0 4 0 6 0 8 0

15:fill

/* 用值填充范围 将val分配给范围[第一个,最后一个)中的所有元素。 template <class ForwardIterator, class T> void fill (ForwardIterator first, ForwardIterator last, const T& val); */ // fill algorithm example #include <iostream> // std::cout #include <algorithm> // std::fill #include <vector> // std::vector int main () { std::vector<int> myvector (8); // myvector: 0 0 0 0 0 0 0 0 std::fill (myvector.begin(),myvector.begin()+4,5); // myvector: 5 5 5 5 0 0 0 0 std::fill (myvector.begin()+3,myvector.end()-2,8); // myvector: 5 5 5 8 8 8 0 0 std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 5 5 5 8 8 8 0 0

16:fill_n

/* 用值填充序列 将val指定给first所指向的序列的前n个元素。 template <class OutputIterator, class Size, class T> OutputIterator fill_n (OutputIterator first, Size n, const T& val); */ // fill_n example #include <iostream> // std::cout #include <algorithm> // std::fill_n #include <vector> // std::vector int main () { std::vector<int> myvector (8,10); // myvector: 10 10 10 10 10 10 10 10 std::fill_n (myvector.begin(),4,20); // myvector: 20 20 20 20 10 10 10 10 std::fill_n (myvector.begin()+3,3,33); // myvector: 20 20 20 33 33 33 10 10 std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 20 20 20 33 33 33 10 10

17:generate

/* 使用函数生成范围的值 将连续调用gen返回的值分配给范围[first,last]中的元素。 template <class ForwardIterator, class Generator> void generate (ForwardIterator first, ForwardIterator last, Generator gen); */ // generate algorithm example #include <iostream> // std::cout #include <algorithm> // std::generate #include <vector> // std::vector #include <ctime> // std::time #include <cstdlib> // std::rand, std::srand // function generator: int RandomNumber () { return (std::rand()%100); } // class generator: struct c_unique { int current; c_unique() {current=0;} int operator()() {return ++current;} } UniqueNumber; int main () { std::srand ( unsigned ( std::time(0) ) ); std::vector<int> myvector (8); std::generate (myvector.begin(), myvector.end(), RandomNumber); std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; std::generate (myvector.begin(), myvector.end(), UniqueNumber); std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } A possible output: myvector contains: 57 87 76 66 85 54 17 15 myvector contains: 1 2 3 4 5 6 7 8

18:generate_n

/* 为具有函数的序列生成值 将连续调用gen返回的值分配给first指向的序列的前n个元素。 template <class OutputIterator, class Size, class Generator> OutputIterator generate_n (OutputIterator first, Size n, Generator gen); */ // generate_n example #include <iostream> // std::cout #include <algorithm> // std::generate_n int current = 0; int UniqueNumber () { return ++current; } int main () { int myarray[9]; std::generate_n (myarray, 9, UniqueNumber); std::cout << "myarray contains:"; for (int i=0; i<9; ++i) std::cout << ' ' << myarray[i]; std::cout << '\n'; return 0; } A possible output: myarray contains: 1 2 3 4 5 6 7 8 9

19:remove

/* 从范围中删除值 template <class ForwardIterator, class T> ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val); */ // remove algorithm example #include <iostream> // std::cout #include <algorithm> // std::remove int main () { int myints[] = {10,20,30,30,20,10,10,20}; // 10 20 30 30 20 10 10 20 // bounds of range: int* pbegin = myints; // ^ int* pend = myints+sizeof(myints)/sizeof(int); // ^ ^ pend = std::remove (pbegin, pend, 20); // 10 30 30 10 10 ? ? ? // ^ ^ std::cout << "range contains:"; for (int* p=pbegin; p!=pend; ++p) std::cout << ' ' << *p; std::cout << '\n'; return 0; } Output: range contains: 10 30 30 10 10

20:remove_if

/* 从范围中删除元素 将范围[first,last)转换为删除了pred返回true的所有元素的范围,并将迭代器返回到该范围的新末尾。 template <class ForwardIterator, class UnaryPredicate> ForwardIterator remove_if (ForwardIterator first, ForwardIterator last, UnaryPredicate pred); */ // remove_if example #include <iostream> // std::cout #include <algorithm> // std::remove_if bool IsOdd (int i) { return ((i%2)==1); } int main () { int myints[] = {1,2,3,4,5,6,7,8,9}; // 1 2 3 4 5 6 7 8 9 // bounds of range: int* pbegin = myints; // ^ int* pend = myints+sizeof(myints)/sizeof(int); // ^ ^ pend = std::remove_if (pbegin, pend, IsOdd); // 2 4 6 8 ? ? ? ? ? // ^ ^ std::cout << "the range contains:"; for (int* p=pbegin; p!=pend; ++p) std::cout << ' ' << *p; std::cout << '\n'; return 0; } Output: the range contains: 2 4 6 8

21:remove_copy

/* 复制范围删除值 将范围[第一个,最后一个)中的元素复制到从result开始的范围,但比较等于val的元素除外。 template <class InputIterator, class OutputIterator, class T> OutputIterator remove_copy (InputIterator first, InputIterator last, OutputIterator result, const T& val); */ // remove_copy example #include <iostream> // std::cout #include <algorithm> // std::remove_copy #include <vector> // std::vector int main () { int myints[] = {10,20,30,30,20,10,10,20}; // 10 20 30 30 20 10 10 20 std::vector<int> myvector (8); std::remove_copy (myints,myints+8,myvector.begin(),20); // 10 30 30 10 10 0 0 0 std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 10 30 30 10 10 0 0 0

22:remove_copy_if

/* 复制范围删除值 将范围[first,last)中的元素复制到从result开始的范围,pred返回true的元素除外。 template <class InputIterator, class OutputIterator, class UnaryPredicate> OutputIterator remove_copy_if (InputIterator first, InputIterator last, OutputIterator result, UnaryPredicate pred); */ // remove_copy_if example #include <iostream> // std::cout #include <algorithm> // std::remove_copy_if #include <vector> // std::vector bool IsOdd (int i) { return ((i%2)==1); } int main () { int myints[] = {1,2,3,4,5,6,7,8,9}; std::vector<int> myvector (9); std::remove_copy_if (myints,myints+9,myvector.begin(),IsOdd); std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 2 4 6 8 0 0 0 0 0

23:unique

/* 删除范围中的连续重复项 从范围[first,last]中的每个连续等效元素组中删除除第一个元素外的所有元素。 equality (1) template <class ForwardIterator> ForwardIterator unique (ForwardIterator first, ForwardIterator last); predicate (2) template <class ForwardIterator, class BinaryPredicate> ForwardIterator unique (ForwardIterator first, ForwardIterator last, BinaryPredicate pred); */ // unique algorithm example #include <iostream> // std::cout #include <algorithm> // std::unique, std::distance #include <vector> // std::vector bool myfunction (int i, int j) { return (i==j); } int main () { int myints[] = {10,20,20,20,30,30,20,20,10}; // 10 20 20 20 30 30 20 20 10 std::vector<int> myvector (myints,myints+9); // using default comparison: std::vector<int>::iterator it; it = std::unique (myvector.begin(), myvector.end()); // 10 20 30 20 10 ? ? ? ? // ^ myvector.resize( std::distance(myvector.begin(),it) ); // 10 20 30 20 10 // using predicate comparison: std::unique (myvector.begin(), myvector.end(), myfunction); // (no changes) // print out content: std::cout << "myvector contains:"; for (it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 10 20 30 20 10

24:unique_copy

/* 复制范围删除重复项 将范围[第一个,最后一个]中的元素复制到从结果开始的范围,但连续重复的元素(与前面的元素相比相等的元素)除外。 equality (1) template <class InputIterator, class OutputIterator> OutputIterator unique_copy (InputIterator first, InputIterator last, OutputIterator result); predicate (2) template <class InputIterator, class OutputIterator, class BinaryPredicate> OutputIterator unique_copy (InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred); */ // unique_copy example #include <iostream> // std::cout #include <algorithm> // std::unique_copy, std::sort, std::distance #include <vector> // std::vector bool myfunction (int i, int j) { return (i==j); } int main () { int myints[] = {10,20,20,20,30,30,20,20,10}; std::vector<int> myvector (9); // 0 0 0 0 0 0 0 0 0 // using default comparison: std::vector<int>::iterator it; it=std::unique_copy (myints,myints+9,myvector.begin()); // 10 20 30 20 10 0 0 0 0 // ^ std::sort (myvector.begin(),it); // 10 10 20 20 30 0 0 0 0 // ^ // using predicate comparison: it=std::unique_copy (myvector.begin(), it, myvector.begin(), myfunction); // 10 20 30 20 30 0 0 0 0 // ^ myvector.resize( std::distance(myvector.begin(),it) ); // 10 20 30 // print out content: std::cout << "myvector contains:"; for (it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 10 20 30

25:reverse

/* 倒车档 转范围[第一个,最后一个)中元素的顺序。 template <class BidirectionalIterator> void reverse (BidirectionalIterator first, BidirectionalIterator last); */ // reverse algorithm example #include <iostream> // std::cout #include <algorithm> // std::reverse #include <vector> // std::vector int main () { std::vector<int> myvector; // set some values: for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 std::reverse(myvector.begin(),myvector.end()); // 9 8 7 6 5 4 3 2 1 // print out content: std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 9 8 7 6 5 4 3 2 1

26:reverse_copy

/* 复制范围反转 将范围[第一个,最后一个)中的元素复制到从结果开始的范围,但顺序相反。 template <class BidirectionalIterator, class OutputIterator> OutputIterator reverse_copy (BidirectionalIterator first, BidirectionalIterator last, OutputIterator result); */ // reverse_copy example #include <iostream> // std::cout #include <algorithm> // std::reverse_copy #include <vector> // std::vector int main () { int myints[] ={1,2,3,4,5,6,7,8,9}; std::vector<int> myvector; myvector.resize(9); // allocate space std::reverse_copy (myints, myints+9, myvector.begin()); // print out content: std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 9 8 7 6 5 4 3 2 1

27:rotate

/* 向左旋转范围内的元素 旋转范围[第一个,最后一个)中元素的顺序,使中间指向的元素成为新的第一个元素。 template <class ForwardIterator> ForwardIterator rotate (ForwardIterator first, ForwardIterator middle, ForwardIterator last); */ // rotate algorithm example #include <iostream> // std::cout #include <algorithm> // std::rotate #include <vector> // std::vector int main () { std::vector<int> myvector; // set some values: for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 std::rotate(myvector.begin(),myvector.begin()+3,myvector.end()); // 4 5 6 7 8 9 1 2 3 // print out content: std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 4 5 6 7 8 9 1 2 3

28:rotate_copy

/* 复制范围向左旋转 将范围[第一个,最后一个)中的元素复制到从结果开始的范围,但旋转元素的顺序,使中间指向的元素成为结果范围中的第一个元素。 template <class ForwardIterator, class OutputIterator> OutputIterator rotate_copy (ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result); */ // rotate_copy algorithm example #include <iostream> // std::cout #include <algorithm> // std::rotate_copy #include <vector> // std::vector int main () { int myints[] = {10,20,30,40,50,60,70}; std::vector<int> myvector (7); std::rotate_copy(myints,myints+3,myints+7,myvector.begin()); // print out content: std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 40 50 60 70 10 20 30

29:random_shuffle

/* 随机重新排列范围中的元素 随机重新排列范围[第一个,最后一个)中的元素。 generator by default (1) template <class RandomAccessIterator> void random_shuffle (RandomAccessIterator first, RandomAccessIterator last); specific generator (2) template <class RandomAccessIterator, class RandomNumberGenerator> void random_shuffle (RandomAccessIterator first, RandomAccessIterator last, RandomNumberGenerator&& gen); */ // random_shuffle example #include <iostream> // std::cout #include <algorithm> // std::random_shuffle #include <vector> // std::vector #include <ctime> // std::time #include <cstdlib> // std::rand, std::srand // random generator function: int myrandom (int i) { return std::rand()%i;} int main () { std::srand ( unsigned ( std::time(0) ) ); std::vector<int> myvector; // set some values: for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 // using built-in random generator: std::random_shuffle ( myvector.begin(), myvector.end() ); // using myrandom: std::random_shuffle ( myvector.begin(), myvector.end(), myrandom); // print out content: std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Possible output: myvector contains: 3 4 1 6 8 9 2 7 5

30:shuffle

/* 使用生成器随机重新排列范围内的元素 使用g作为均匀随机数生成器,随机重新排列范围[第一个,最后一个)中的元素。 template <class RandomAccessIterator, class URNG> void shuffle (RandomAccessIterator first, RandomAccessIterator last, URNG&& g); */ // shuffle algorithm example #include <iostream> // std::cout #include <algorithm> // std::shuffle #include <array> // std::array #include <random> // std::default_random_engine #include <chrono> // std::chrono::system_clock int main () { std::array<int,5> foo {1,2,3,4,5}; // obtain a time-based seed: unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); shuffle (foo.begin(), foo.end(), std::default_random_engine(seed)); std::cout << "shuffled elements:"; for (int& x: foo) std::cout << ' ' << x; std::cout << '\n'; return 0; } Possible output: shuffled elements: 3 1 4 2 5

第三部分:分区

1:is_partitioned

/* 测试范围是否分区 如果pred返回true的范围[first,last)中的所有元素都在其返回false的元素之前,则返回true。 template <class InputIterator, class UnaryPredicate> bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred); */ // is_partitioned example #include <iostream> // std::cout #include <algorithm> // std::is_partitioned #include <array> // std::array bool IsOdd (int i) { return (i%2)==1; } int main () { std::array<int,7> foo {1,2,3,4,5,6,7}; // print contents: std::cout << "foo:"; for (int& x:foo) std::cout << ' ' << x; if ( std::is_partitioned(foo.begin(),foo.end(),IsOdd) ) std::cout << " (partitioned)\n"; else std::cout << " (not partitioned)\n"; // partition array: std::partition (foo.begin(),foo.end(),IsOdd); // print contents again: std::cout << "foo:"; for (int& x:foo) std::cout << ' ' << x; if ( std::is_partitioned(foo.begin(),foo.end(),IsOdd) ) std::cout << " (partitioned)\n"; else std::cout << " (not partitioned)\n"; return 0; } Possible output: foo: 1 2 3 4 5 6 7 (not partitioned) foo: 1 7 3 5 4 6 2 (partitioned)

2:partition

/* 分区范围为二 重新排列范围[first,last)中的元素,使pred返回true的所有元素都在其返回false的所有元素之前。迭代器返回的点指向第二组的第一个元素。 template <class ForwardIterator, class UnaryPredicate> ForwardIterator partition (ForwardIterator first, ForwardIterator last, UnaryPredicate pred); */ // partition algorithm example #include <iostream> // std::cout #include <algorithm> // std::partition #include <vector> // std::vector bool IsOdd (int i) { return (i%2)==1; } int main () { std::vector<int> myvector; // set some values: for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 std::vector<int>::iterator bound; bound = std::partition (myvector.begin(), myvector.end(), IsOdd); // print out content: std::cout << "odd elements:"; for (std::vector<int>::iterator it=myvector.begin(); it!=bound; ++it) std::cout << ' ' << *it; std::cout << '\n'; std::cout << "even elements:"; for (std::vector<int>::iterator it=bound; it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Possible output: odd elements: 1 9 3 7 5 even elements: 6 4 8 2

3:stable_partition

/* 二稳定排序中的划分范围 重新排列范围[第一个,最后一个)中的元素,使pred返回true的所有元素都在其返回false的所有元素之前,并且与函数划分不同,每个组中元素的相对顺序都得到了保留。 template <class BidirectionalIterator, class UnaryPredicate> BidirectionalIterator stable_partition (BidirectionalIterator first, BidirectionalIterator last, UnaryPredicate pred); */ // stable_partition example #include <iostream> // std::cout #include <algorithm> // std::stable_partition #include <vector> // std::vector bool IsOdd (int i) { return (i%2)==1; } int main () { std::vector<int> myvector; // set some values: for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 std::vector<int>::iterator bound; bound = std::stable_partition (myvector.begin(), myvector.end(), IsOdd); // print out content: std::cout << "odd elements:"; for (std::vector<int>::iterator it=myvector.begin(); it!=bound; ++it) std::cout << ' ' << *it; std::cout << '\n'; std::cout << "even elements:"; for (std::vector<int>::iterator it=bound; it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: odd elements: 1 3 5 7 9 even elements: 2 4 6 8

4:partition_copy

/* 将范围划分为两部分 将pred返回true的范围[first,last)中的元素复制到result_true所指向的范围中,以及将pred未返回true的元素复制在result_false所指向范围中。 template <class InputIterator, class OutputIterator1, class OutputIterator2, class UnaryPredicate pred> pair<OutputIterator1,OutputIterator2> partition_copy (InputIterator first, InputIterator last, OutputIterator1 result_true, OutputIterator2 result_false, UnaryPredicate pred); */ // partition_copy example #include <iostream> // std::cout #include <algorithm> // std::partition_copy, std::count_if #include <vector> // std::vector bool IsOdd (int i) { return (i%2)==1; } int main () { std::vector<int> foo {1,2,3,4,5,6,7,8,9}; std::vector<int> odd, even; // resize vectors to proper size: unsigned n = std::count_if (foo.begin(), foo.end(), IsOdd); odd.resize(n); even.resize(foo.size()-n); // partition: std::partition_copy (foo.begin(), foo.end(), odd.begin(), even.begin(), IsOdd); // print contents: std::cout << "odd: "; for (int& x:odd) std::cout << ' ' << x; std::cout << '\n'; std::cout << "even: "; for (int& x:even) std::cout << ' ' << x; std::cout << '\n'; return 0; } Output: odd: 1 3 5 7 9 even: 2 4 6 8

5:partition_point

/* 获取分区点 将迭代器返回到分区范围[first,last)中pred不为true的第一个元素,指示其分区点。 template <class ForwardIterator, class UnaryPredicate> ForwardIterator partition_point (ForwardIterator first, ForwardIterator last, UnaryPredicate pred); */ // partition_point example #include <iostream> // std::cout #include <algorithm> // std::partition, std::partition_point #include <vector> // std::vector bool IsOdd (int i) { return (i%2)==1; } int main () { std::vector<int> foo {1,2,3,4,5,6,7,8,9}; std::vector<int> odd; std::partition (foo.begin(),foo.end(),IsOdd); auto it = std::partition_point(foo.begin(),foo.end(),IsOdd); odd.assign (foo.begin(),it); // print contents of odd: std::cout << "odd:"; for (int& x:odd) std::cout << ' ' << x; std::cout << '\n'; return 0; } Output: odd: 1 3 5 7 9

第四部分:排序

1:sort

/* 对范围内的元素排序 按升序对范围[第一个,最后一个)中的元素进行排序。 default (1) template <class RandomAccessIterator> void sort (RandomAccessIterator first, RandomAccessIterator last); custom (2) template <class RandomAccessIterator, class Compare> void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp); */ // sort algorithm example #include <iostream> // std::cout #include <algorithm> // std::sort #include <vector> // std::vector bool myfunction (int i,int j) { return (i<j); } struct myclass { bool operator() (int i,int j) { return (i<j);} } myobject; int main () { int myints[] = {32,71,12,45,26,80,53,33}; std::vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33 // using default comparison (operator <): std::sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33 // using function as comp std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80) // using object as comp std::sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80) // print out content: std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 12 26 32 33 45 53 71 80

2:stable_sort

/* 对保持等价顺序的元素进行排序 将范围[第一个,最后一个)中的元素按升序排序,类似于排序,但stable_sort保留了具有等效值的元素的相对顺序。 template <class RandomAccessIterator> void stable_sort ( RandomAccessIterator first, RandomAccessIterator last ); template <class RandomAccessIterator, class Compare> void stable_sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp ); */ // stable_sort example #include <iostream> // std::cout #include <algorithm> // std::stable_sort #include <vector> // std::vector bool compare_as_ints (double i,double j) { return (int(i)<int(j)); } int main () { double mydoubles[] = {3.14, 1.41, 2.72, 4.67, 1.73, 1.32, 1.62, 2.58}; std::vector<double> myvector; myvector.assign(mydoubles,mydoubles+8); std::cout << "using default comparison:"; std::stable_sort (myvector.begin(), myvector.end()); for (std::vector<double>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; myvector.assign(mydoubles,mydoubles+8); std::cout << "using 'compare_as_ints' :"; std::stable_sort (myvector.begin(), myvector.end(), compare_as_ints); for (std::vector<double>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Possible output: using default comparison: 1.32 1.41 1.62 1.73 2.58 2.72 3.14 4.67 using 'compare_as_ints' : 1.41 1.73 1.32 1.62 2.72 2.58 3.14 4.67

3:partial_sort

/* 对范围中的元素进行部分排序 重新排列范围[第一个,最后一个)中的元素,使中间之前的元素是整个范围中最小的元素,并按升序排序,而其余元素则没有任何特定顺序。 default (1) template <class RandomAccessIterator> void partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last); custom (2) template <class RandomAccessIterator, class Compare> void partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp); */ // partial_sort example #include <iostream> // std::cout #include <algorithm> // std::partial_sort #include <vector> // std::vector bool myfunction (int i,int j) { return (i<j); } int main () { int myints[] = {9,8,7,6,5,4,3,2,1}; std::vector<int> myvector (myints, myints+9); // using default comparison (operator <): std::partial_sort (myvector.begin(), myvector.begin()+5, myvector.end()); // using function as comp std::partial_sort (myvector.begin(), myvector.begin()+5, myvector.end(),myfunction); // print out content: std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Possible output: myvector contains: 1 2 3 4 5 9 8 7 6

4:partial_sort_copy

/* 复制并部分排序范围 将范围[first,last)到[result_first,result_last)中的最小元素复制,并对复制的元素进行排序。复制的元素数与result_firs特和result_last之间的距离相同(除非这大于[first、last)中的元素数)。 default (1) template <class InputIterator, class RandomAccessIterator> RandomAccessIterator partial_sort_copy (InputIterator first,InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last); custom (2) template <class InputIterator, class RandomAccessIterator, class Compare> RandomAccessIterator partial_sort_copy (InputIterator first,InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp); */ // partial_sort_copy example #include <iostream> // std::cout #include <algorithm> // std::partial_sort_copy #include <vector> // std::vector bool myfunction (int i,int j) { return (i<j); } int main () { int myints[] = {9,8,7,6,5,4,3,2,1}; std::vector<int> myvector (5); // using default comparison (operator <): std::partial_sort_copy (myints, myints+9, myvector.begin(), myvector.end()); // using function as comp std::partial_sort_copy (myints, myints+9, myvector.begin(), myvector.end(), myfunction); // print out content: std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: myvector contains: 1 2 3 4 5

5:is_sorted

/* 检查范围是否排序 如果范围[first,last)按升序排序,则返回true。 default (1) template <class ForwardIterator> bool is_sorted (ForwardIterator first, ForwardIterator last); custom (2) template <class ForwardIterator, class Compare> bool is_sorted (ForwardIterator first, ForwardIterator last, Compare comp); */ // is_sorted example #include <iostream> // std::cout #include <algorithm> // std::is_sorted, std::prev_permutation #include <array> // std::array int main () { std::array<int,4> foo {2,4,1,3}; do { // try a new permutation: std::prev_permutation(foo.begin(),foo.end()); // print range: std::cout << "foo:"; for (int& x:foo) std::cout << ' ' << x; std::cout << '\n'; } while (!std::is_sorted(foo.begin(),foo.end())); std::cout << "the range is sorted!\n"; return 0; } Output: foo: 2 3 4 1 foo: 2 3 1 4 foo: 2 1 4 3 foo: 2 1 3 4 foo: 1 4 3 2 foo: 1 4 2 3 foo: 1 3 4 2 foo: 1 3 2 4 foo: 1 2 4 3 foo: 1 2 3 4 the range is sorted!|

6:is_sorted_until

/* 查找范围中第一个未排序的元素 将迭代器返回到范围[first,last)中不遵循升序的第一个元素。 default (1) template <class ForwardIterator> ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last); custom (2) template <class ForwardIterator, class Compare> ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last, Compare comp); */ // is_sorted_until example #include <iostream> // std::cout #include <algorithm> // std::is_sorted_until, std::prev_permutation #include <array> // std::array int main () { std::array<int,4> foo {2,4,1,3}; std::array<int,4>::iterator it; do { // try a new permutation: std::prev_permutation(foo.begin(),foo.end()); // print range: std::cout << "foo:"; for (int& x:foo) std::cout << ' ' << x; it=std::is_sorted_until(foo.begin(),foo.end()); std::cout << " (" << (it-foo.begin()) << " elements sorted)\n"; } while (it!=foo.end()); std::cout << "the range is sorted!\n"; return 0; } Output: foo: 2 3 4 1 (3 elements sorted) foo: 2 3 1 4 (2 elements sorted) foo: 2 1 4 3 (1 elements sorted) foo: 2 1 3 4 (1 elements sorted) foo: 1 4 3 2 (2 elements sorted) foo: 1 4 2 3 (2 elements sorted) foo: 1 3 4 2 (3 elements sorted) foo: 1 3 2 4 (2 elements sorted) foo: 1 2 4 3 (3 elements sorted) foo: 1 2 3 4 (4 elements sorted) the range is sorted!|

7:nth_element

/* 对范围中的元素排序 重新排列范围[第一个,最后一个)中的元素,使第n个位置的元素是排序序列中该位置的元素。 default (1) template <class RandomAccessIterator> void nth_element (RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last); custom (2) template <class RandomAccessIterator, class Compare> void nth_element (RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp); */ // nth_element example #include <iostream> // std::cout #include <algorithm> // std::nth_element, std::random_shuffle #include <vector> // std::vector bool myfunction (int i,int j) { return (i<j); } int main () { std::vector<int> myvector; // set some values: for (int i=1; i<10; i++) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 std::random_shuffle (myvector.begin(), myvector.end()); // using default comparison (operator <): std::nth_element (myvector.begin(), myvector.begin()+5, myvector.end()); // using function as comp std::nth_element (myvector.begin(), myvector.begin()+5, myvector.end(),myfunction); // print out content: std::cout << "myvector contains:"; for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Possible output: myvector contains: 3 1 4 2 5 6 9 7 8

第五部分:二进制搜索

1:lower_bound

/* 将迭代器返回到下界 返回一个迭代器,该迭代器指向范围[first,last)中的第一个元素,该元素的比较值不小于val。 default (1) template <class ForwardIterator, class T> ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val); custom (2) template <class ForwardIterator, class T, class Compare> ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val, Compare comp); */ // lower_bound/upper_bound example #include <iostream> // std::cout #include <algorithm> // std::lower_bound, std::upper_bound, std::sort #include <vector> // std::vector int main () { int myints[] = {10,20,30,30,20,10,10,20}; std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20 std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 std::vector<int>::iterator low,up; low=std::lower_bound (v.begin(), v.end(), 20); // ^ up= std::upper_bound (v.begin(), v.end(), 20); // ^ std::cout << "lower_bound at position " << (low- v.begin()) << '\n'; std::cout << "upper_bound at position " << (up - v.begin()) << '\n'; return 0; } Output: lower_bound at position 3 upper_bound at position 6

2:upper_bound

/* 将迭代器返回到上限 返回一个迭代器,该迭代器指向范围[first,last)中比较值大于val的第一个元素。 default (1) template <class ForwardIterator, class T> ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T& val); custom (2) template <class ForwardIterator, class T, class Compare> ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T& val, Compare comp); */ // lower_bound/upper_bound example #include <iostream> // std::cout #include <algorithm> // std::lower_bound, std::upper_bound, std::sort #include <vector> // std::vector int main () { int myints[] = {10,20,30,30,20,10,10,20}; std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20 std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 std::vector<int>::iterator low,up; low=std::lower_bound (v.begin(), v.end(), 20); // ^ up= std::upper_bound (v.begin(), v.end(), 20); // ^ std::cout << "lower_bound at position " << (low- v.begin()) << '\n'; std::cout << "upper_bound at position " << (up - v.begin()) << '\n'; return 0; } Output: lower_bound at position 3 upper_bound at position 6

3:equal_range

/* 获取相等元素的子范围 返回子范围的边界,该子范围包括值等于val[第一个,最后一个]范围的所有元素。 default (1) template <class ForwardIterator, class T> pair<ForwardIterator,ForwardIterator> equal_range (ForwardIterator first, ForwardIterator last, const T& val); custom (2) template <class ForwardIterator, class T, class Compare> pair<ForwardIterator,ForwardIterator> equal_range (ForwardIterator first, ForwardIterator last, const T& val, Compare comp); */ // equal_range example // equal_range example #include <iostream> // std::cout #include <algorithm> // std::equal_range, std::sort #include <vector> // std::vector bool mygreater (int i,int j) { return (i>j); } int main () { int myints[] = {10,20,30,30,20,10,10,20}; std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20 std::pair<std::vector<int>::iterator,std::vector<int>::iterator> bounds; // using default comparison: std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 bounds=std::equal_range (v.begin(), v.end(), 20); // ^ ^ // using "mygreater" as comp: std::sort (v.begin(), v.end(), mygreater); // 30 30 20 20 20 10 10 10 bounds=std::equal_range (v.begin(), v.end(), 20, mygreater); // ^ ^ std::cout << "bounds at positions " << (bounds.first - v.begin()); std::cout << " and " << (bounds.second - v.begin()) << '\n'; return 0; } Output: bounds at positions 2 and 5

4:binary_search

/* 测试值是否存在于排序序列中 如果范围[first,last)中的任何元素等效于val,则返回true,否则返回false。 default (1) template <class ForwardIterator, class T> bool binary_search (ForwardIterator first, ForwardIterator last, const T& val); custom (2) template <class ForwardIterator, class T, class Compare> bool binary_search (ForwardIterator first, ForwardIterator last, const T& val, Compare comp); */ // binary_search example #include <iostream> // std::cout #include <algorithm> // std::binary_search, std::sort #include <vector> // std::vector bool myfunction (int i,int j) { return (i<j); } int main () { int myints[] = {1,2,3,4,5,4,3,2,1}; std::vector<int> v(myints,myints+9); // 1 2 3 4 5 4 3 2 1 // using default comparison: std::sort (v.begin(), v.end()); std::cout << "looking for a 3... "; if (std::binary_search (v.begin(), v.end(), 3)) std::cout << "found!\n"; else std::cout << "not found.\n"; // using myfunction as comp: std::sort (v.begin(), v.end(), myfunction); std::cout << "looking for a 6... "; if (std::binary_search (v.begin(), v.end(), 6, myfunction)) std::cout << "found!\n"; else std::cout << "not found.\n"; return 0; } Output: looking for a 3... found! looking for a 6... not found.

第六部分:合并

1:merge

/* 合并已排序的范围 将排序范围[first1,last1)和[first2,last2)中的元素组合到一个新的范围中,从result开始,所有元素都已排序。 default (1) template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator merge (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); custom (2) template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> OutputIterator merge (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); */ // merge algorithm example #include <iostream> // std::cout #include <algorithm> // std::merge, std::sort #include <vector> // std::vector int main () { int first[] = {5,10,15,20,25}; int second[] = {50,40,30,20,10}; std::vector<int> v(10); std::sort (first,first+5); std::sort (second,second+5); std::merge (first,first+5,second,second+5,v.begin()); std::cout << "The resulting vector contains:"; for (std::vector<int>::iterator it=v.begin(); it!=v.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: The resulting vector contains: 5 10 10 15 20 20 25 30 40 50

2:inplace_merge

/* 合并连续排序的范围 合并两个连续的排序范围:[第一,中间)和[中间,最后),将结果放入组合的排序范围[第一,最后)。 default (1) template <class BidirectionalIterator> void inplace_merge (BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last); custom (2) template <class BidirectionalIterator, class Compare> void inplace_merge (BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp); */ // inplace_merge example #include <iostream> // std::cout #include <algorithm> // std::inplace_merge, std::sort, std::copy #include <vector> // std::vector int main () { int first[] = {5,10,15,20,25}; int second[] = {50,40,30,20,10}; std::vector<int> v(10); std::vector<int>::iterator it; std::sort (first,first+5); std::sort (second,second+5); it=std::copy (first, first+5, v.begin()); std::copy (second,second+5,it); std::inplace_merge (v.begin(),v.begin()+5,v.end()); std::cout << "The resulting vector contains:"; for (it=v.begin(); it!=v.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: The resulting vector contains: 5 10 10 15 20 20 25 30 40 50

3:includes

/* 测试排序的范围是否包括另一个排序的范围 如果排序的范围[first1,last1)包含排序的范围[first2,last2)中的所有元素,则返回true。 template <class InputIterator1, class InputIterator2> bool includes ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2 ); template <class InputIterator1, class InputIterator2, class Compare> bool includes ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp ); */ // includes algorithm example #include <iostream> // std::cout #include <algorithm> // std::includes, std::sort bool myfunction (int i, int j) { return i<j; } int main () { int container[] = {5,10,15,20,25,30,35,40,45,50}; int continent[] = {40,30,20,10}; std::sort (container,container+10); std::sort (continent,continent+4); // using default comparison: if ( std::includes(container,container+10,continent,continent+4) ) std::cout << "container includes continent!\n"; // using myfunction as comp: if ( std::includes(container,container+10,continent,continent+4, myfunction) ) std::cout << "container includes continent!\n"; return 0; } Output: container includes continent! container includes continent!

4:set_union

/* 两个排序范围的并集 用两个排序范围[first1,last1)和[first2,last2)的集合并集,构造一个从result指向的位置开始的排序范围。 default (1) template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_union (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); custom (2) template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> OutputIterator set_union (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); */ // set_union example #include <iostream> // std::cout #include <algorithm> // std::set_union, std::sort #include <vector> // std::vector int main () { int first[] = {5,10,15,20,25}; int second[] = {50,40,30,20,10}; std::vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0 std::vector<int>::iterator it; std::sort (first,first+5); // 5 10 15 20 25 std::sort (second,second+5); // 10 20 30 40 50 it=std::set_union (first, first+5, second, second+5, v.begin()); // 5 10 15 20 25 30 40 50 0 0 v.resize(it-v.begin()); // 5 10 15 20 25 30 40 50 std::cout << "The union has " << (v.size()) << " elements:\n"; for (it=v.begin(); it!=v.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: The union has 8 elements: 5 10 15 20 25 30 40 50|

5:set_intersection

/* 两个排序范围的交集 构造一个排序范围,从result指向的位置开始,并设置两个排序范围[first1,last1)和[first2,last2)的交集。 default (1) template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); custom (2) template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); */ // set_intersection example #include <iostream> // std::cout #include <algorithm> // std::set_intersection, std::sort #include <vector> // std::vector int main () { int first[] = {5,10,15,20,25}; int second[] = {50,40,30,20,10}; std::vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0 std::vector<int>::iterator it; std::sort (first,first+5); // 5 10 15 20 25 std::sort (second,second+5); // 10 20 30 40 50 it=std::set_intersection (first, first+5, second, second+5, v.begin()); // 10 20 0 0 0 0 0 0 0 0 v.resize(it-v.begin()); // 10 20 std::cout << "The intersection has " << (v.size()) << " elements:\n"; for (it=v.begin(); it!=v.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: The intersection has 2 elements: 10 20

6:set_difference

/* 两个排序范围的差异 构造一个排序范围,该排序范围从result指向的位置开始,具有排序范围[first1,last1)相对于排序范围[f1st2,last2)的集合差。 default (1) template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); custom (2) template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); */ // set_difference example #include <iostream> // std::cout #include <algorithm> // std::set_difference, std::sort #include <vector> // std::vector int main () { int first[] = {5,10,15,20,25}; int second[] = {50,40,30,20,10}; std::vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0 std::vector<int>::iterator it; std::sort (first,first+5); // 5 10 15 20 25 std::sort (second,second+5); // 10 20 30 40 50 it=std::set_difference (first, first+5, second, second+5, v.begin()); // 5 15 25 0 0 0 0 0 0 0 v.resize(it-v.begin()); // 5 15 25 std::cout << "The difference has " << (v.size()) << " elements:\n"; for (it=v.begin(); it!=v.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: The difference has 3 elements: 5 15 25|

7:set_symmetric_difference

/* 两个排序范围的对称差 用两个排序范围[first1,last1)和[first2,last2)的集对称差,构造一个从result所指向的位置开始的排序范围。 default (1) template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); custom (2) template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); */ // set_symmetric_difference example #include <iostream> // std::cout #include <algorithm> // std::set_symmetric_difference, std::sort #include <vector> // std::vector int main () { int first[] = {5,10,15,20,25}; int second[] = {50,40,30,20,10}; std::vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0 std::vector<int>::iterator it; std::sort (first,first+5); // 5 10 15 20 25 std::sort (second,second+5); // 10 20 30 40 50 it=std::set_symmetric_difference (first, first+5, second, second+5, v.begin()); // 5 15 25 30 40 50 0 0 0 0 v.resize(it-v.begin()); // 5 15 25 30 40 50 std::cout << "The symmetric difference has " << (v.size()) << " elements:\n"; for (it=v.begin(); it!=v.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } Output: The symmetric difference has 6 elements: 5 15 25 30 40 50

第六部分:堆

1:push_heap

/* 将元素推入堆范围 给定范围为[first,last1)的堆,此函数通过将(last1)中的值放在堆的相应位置,将堆的范围扩展到[first、last)。 default (1) template <class RandomAccessIterator> void push_heap (RandomAccessIterator first, RandomAccessIterator last); custom (2) template <class RandomAccessIterator, class Compare> void push_heap (RandomAccessIterator first, RandomAccessIterator last, Compare comp); */ // range heap example #include <iostream> // std::cout #include <algorithm> // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap #include <vector> // std::vector int main () { int myints[] = {10,20,30,5,15}; std::vector<int> v(myints,myints+5); std::make_heap (v.begin(),v.end()); std::cout << "initial max heap : " << v.front() << '\n'; std::pop_heap (v.begin(),v.end()); v.pop_back(); std::cout << "max heap after pop : " << v.front() << '\n'; v.push_back(99); std::push_heap (v.begin(),v.end()); std::cout << "max heap after push: " << v.front() << '\n'; std::sort_heap (v.begin(),v.end()); std::cout << "final sorted range :"; for (unsigned i=0; i<v.size(); i++) std::cout << ' ' << v[i]; std::cout << '\n'; return 0; } Output: initial max heap : 30 max heap after pop : 20 max heap after push: 99 final sorted range : 5 10 15 20 99

2:pop_heap

3:make_heap

4:sort_heap

5:is_heap

6:is_heap_until

第七部分:极值

1:min

/* 返回a和b中最小的一个。如果两者相等,则返回a。 default (1) template <class T> const T& min (const T& a, const T& b); custom (2) template <class T, class Compare> const T& min (const T& a, const T& b, Compare comp); initializer list (3) template <class T> T min (initializer_list<T> il); template <class T, class Compare> T min (initializer_list<T> il, Compare comp); */ // min example #include <iostream> // std::cout #include <algorithm> // std::min int main () { std::cout << "min(1,2)==" << std::min(1,2) << '\n'; std::cout << "min(2,1)==" << std::min(2,1) << '\n'; std::cout << "min('a','z')==" << std::min('a','z') << '\n'; std::cout << "min(3.14,2.72)==" << std::min(3.14,2.72) << '\n'; return 0; } Output: min(1,2)==1 min(2,1)==1 min('a','z')==a min(3.14,2.72)==2.72

2:max

/* 返回a和b中的最大值。如果两者相等,则返回a。 default (1) template <class T> const T& max (const T& a, const T& b); custom (2) template <class T, class Compare> const T& max (const T& a, const T& b, Compare comp); initializer list (3) template <class T> T max (initializer_list<T> il); template <class T, class Compare> T max (initializer_list<T> il, Compare comp); */ // max example #include <iostream> // std::cout #include <algorithm> // std::max int main () { std::cout << "max(1,2)==" << std::max(1,2) << '\n'; std::cout << "max(2,1)==" << std::max(2,1) << '\n'; std::cout << "max('a','z')==" << std::max('a','z') << '\n'; std::cout << "max(3.14,2.73)==" << std::max(3.14,2.73) << '\n'; return 0; } Output: max(1,2)==2 max(2,1)==2 max('a','z')==z max(3.14,2.73)==3.14

3:minmax

/* 返回最小和最大元素 返回一对,其中a和b中最小的作为第一个元素,最大的作为第二个元素。如果两者等效,则函数返回make_pair(a,b)。 default (1) template <class T> pair <const T&,const T&> minmax (const T& a, const T& b); custom (2) template <class T, class Compare> pair <const T&,const T&> minmax (const T& a, const T& b, Compare comp); initializer list (3) template <class T> pair<T,T> minmax (initializer_list<T> il); template <class T, class Compare> pair<T,T> minmax (initializer_list<T> il, Compare comp); */ // minmax example #include <iostream> // std::cout #include <algorithm> // std::minmax int main () { auto result = std::minmax({1,2,3,4,5}); std::cout << "minmax({1,2,3,4,5}): "; std::cout << result.first << ' ' << result.second << '\n'; return 0; } Output: minmax({1,2,3,4,5}): 1 5

4:min_element

/* 返回范围内的最小元素 返回一个迭代器,该迭代器指向范围[first,last)中值最小的元素。 default (1) template <class ForwardIterator> ForwardIterator min_element (ForwardIterator first, ForwardIterator last); custom (2) template <class ForwardIterator, class Compare> ForwardIterator min_element (ForwardIterator first, ForwardIterator last, Compare comp); */ // min_element/max_element example #include <iostream> // std::cout #include <algorithm> // std::min_element, std::max_element bool myfn(int i, int j) { return i<j; } struct myclass { bool operator() (int i,int j) { return i<j; } } myobj; int main () { int myints[] = {3,7,2,5,6,4,9}; // using default comparison: std::cout << "The smallest element is " << *std::min_element(myints,myints+7) << '\n'; std::cout << "The largest element is " << *std::max_element(myints,myints+7) << '\n'; // using function myfn as comp: std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myfn) << '\n'; std::cout << "The largest element is " << *std::max_element(myints,myints+7,myfn) << '\n'; // using object myobj as comp: std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myobj) << '\n'; std::cout << "The largest element is " << *std::max_element(myints,myints+7,myobj) << '\n'; return 0; } Output: The smallest element is 2 The largest element is 9 The smallest element is 2 The largest element is 9 The smallest element is 2 The largest element is 9

5:max_element

/* 返回范围内的最大元素 返回一个迭代器,该迭代器指向范围[first,last]中值最大的元素。 default (1) template <class ForwardIterator> ForwardIterator max_element (ForwardIterator first, ForwardIterator last); custom (2) template <class ForwardIterator, class Compare> ForwardIterator max_element (ForwardIterator first, ForwardIterator last, Compare comp); */ // min_element/max_element example #include <iostream> // std::cout #include <algorithm> // std::min_element, std::max_element bool myfn(int i, int j) { return i<j; } struct myclass { bool operator() (int i,int j) { return i<j; } } myobj; int main () { int myints[] = {3,7,2,5,6,4,9}; // using default comparison: std::cout << "The smallest element is " << *std::min_element(myints,myints+7) << '\n'; std::cout << "The largest element is " << *std::max_element(myints,myints+7) << '\n'; // using function myfn as comp: std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myfn) << '\n'; std::cout << "The largest element is " << *std::max_element(myints,myints+7,myfn) << '\n'; // using object myobj as comp: std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myobj) << '\n'; std::cout << "The largest element is " << *std::max_element(myints,myints+7,myobj) << '\n'; return 0; } Output: The smallest element is 2 The largest element is 9 The smallest element is 2 The largest element is 9 The smallest element is 2 The largest element is 9

6:minmax_element

/* 返回范围内最小和最大的元素 返回一对,迭代器指向范围[first,last)中最小值作为第一个元素,最大值作为第二个元素的元素。 default (1) template <class ForwardIterator> pair<ForwardIterator,ForwardIterator> minmax_element (ForwardIterator first, ForwardIterator last); custom (2) template <class ForwardIterator, class Compare> pair<ForwardIterator,ForwardIterator> minmax_element (ForwardIterator first, ForwardIterator last, Compare comp); */ // minmax_element #include <iostream> // std::cout #include <algorithm> // std::minmax_element #include <array> // std::array int main () { std::array<int,7> foo {3,7,2,9,5,8,6}; auto result = std::minmax_element (foo.begin(),foo.end()); // print result: std::cout << "min is " << *result.first; std::cout << ", at position " << (result.first-foo.begin()) << '\n'; std::cout << "max is " << *result.second; std::cout << ", at position " << (result.second-foo.begin()) << '\n'; return 0; } Output: min is 2, at position 2 max is 9, at position 3

第八部分:其他

1:lexicographical_compare

/* 词汇学少于比较 如果范围[first1,last1)在字典上的比较小于范围[first2,last2),则返回true。 default (1) template <class InputIterator1, class InputIterator2> bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); custom (2) template <class InputIterator1, class InputIterator2, class Compare> bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp); */ // lexicographical_compare example #include <iostream> // std::cout, std::boolalpha #include <algorithm> // std::lexicographical_compare #include <cctype> // std::tolower // a case-insensitive comparison function: bool mycomp (char c1, char c2) { return std::tolower(c1)<std::tolower(c2); } int main () { char foo[]="Apple"; char bar[]="apartment"; std::cout << std::boolalpha; std::cout << "Comparing foo and bar lexicographically (foo<bar):\n"; std::cout << "Using default comparison (operator<): "; std::cout << std::lexicographical_compare(foo,foo+5,bar,bar+9); std::cout << '\n'; std::cout << "Using mycomp as comparison object: "; std::cout << std::lexicographical_compare(foo,foo+5,bar,bar+9,mycomp); std::cout << '\n'; return 0; } Output: Comparing foo and bar lexicographically (foo<bar): Using default comparison (operator<): true Using mycomp as comparison object: false

2:next_permutation

/* 将范围转换为下一个排列 将范围[第一个,最后一个)中的元素重新排列到下一个字典上更大的排列中 default (1) template <class BidirectionalIterator> bool next_permutation (BidirectionalIterator first, BidirectionalIterator last); custom (2) template <class BidirectionalIterator, class Compare> bool next_permutation (BidirectionalIterator first, BidirectionalIterator last, Compare comp); */ // next_permutation example #include <iostream> // std::cout #include <algorithm> // std::next_permutation, std::sort int main () { int myints[] = {1,2,3}; std::sort (myints,myints+3); std::cout << "The 3! possible permutations with 3 elements:\n"; do { std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n'; } while ( std::next_permutation(myints,myints+3) ); std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n'; return 0; } Output: The 3! possible permutations with 3 elements: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 After loop: 1 2 3

3:prev_permutation

/* 将范围转换为以前的排列 将范围[第一个,最后一个)中的元素重新排列到先前的字典排序排列中。 default (1) template <class BidirectionalIterator> bool prev_permutation (BidirectionalIterator first, BidirectionalIterator last ); custom (2) template <class BidirectionalIterator, class Compare> bool prev_permutation (BidirectionalIterator first, BidirectionalIterator last, Compare comp); */ // next_permutation example #include <iostream> // std::cout #include <algorithm> // std::next_permutation, std::sort, std::reverse int main () { int myints[] = {1,2,3}; std::sort (myints,myints+3); std::reverse (myints,myints+3); std::cout << "The 3! possible permutations with 3 elements:\n"; do { std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n'; } while ( std::prev_permutation(myints,myints+3) ); std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n'; return 0; } Output: 3 2 1 3 1 2 2 3 1 2 1 3 1 3 2 1 2 3 After loop: 3 2 1
  • C++

    C++ 是在 C 语言的基础上开发的一种通用编程语言,应用广泛。C++ 支持多种编程范式,面向对象编程、泛型编程和过程化编程。

    108 引用 • 153 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...