头文件:<algorithm>
-
all_of
template <class InputIterator, class UnaryPredicate> bool all_of(InputIterator first, InputIterator last, UnaryPredicate pred);
解释:如果 pred 对范围 [first,last) 中的所有元素返回true或者范围为空,则返回true,否则返回false。
例子:
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";
-
any_of
template <class InputIterator, class UnaryPredicate> bool any_of(InputIterator first, InputIterator last, UnaryPredicate pred);
解释:如果 pred 对范围[first,last) 中的任何一个元素返回true,则返回true,否则返回false。 如果[first,last)是一个空范围,则该函数返回false。
例子:
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";
-
none_of
template <class InputIterator, class UnaryPredicate> bool none_of(InputIterator first, InputIterator last, UnaryPredicate pred);
解释:如果 pred 对范围[first,last)中的所有元素返回false或者范围为空,则返回true,否则返回false。
例子:
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";
-
for_each
template <class InputIterator, class Function> Function for_each(InputIterator first, InputIterator last, Function fn);
解释:将函数 fn 应用于[first,last)范围内的每个元素。
例子:
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); // 1 std::cout << '\n'; // or: std::cout << "myvector contains:"; for_each (myvector.begin(), myvector.end(), myobject); // 2 std::cout << '\n'; return 0; }
-
find
template <class InputIterator, class T> InputIterator find(InputIterator first, InputIterator last, const T& val);
解释:返回一个迭代器指向 [first, last) 范围中第一个等于 val 的元素;如果没有找到,则返回 last。该函数使用 operator== 将各个元素与val进行比较。
例子:
// 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";
-
find_if
template <class InputIterator, class UnaryPredicate> InputIterator find_if(InputIterator first, InputIterator last, UnaryPredicate pred);
解释:返回一个迭代器指向 [first, last)中第一个令 pred 返回 true的元素;如果没有找到,则返回 last.
例子:
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; }
-
find_if_not
template <class InputIterator, class UnaryPredicate> InputIterator find_if_not(InputIterator first, InputIterator last, UnaryPredicate pred);
解释:返回一个迭代器指向 [first, last)中第一个令 pred 返回 false 的元素;如果没有找到,则返回 last.
例子:
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';
-
find_end
// 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);
解释:[first2,last2)定义了一个要查找的目标序列,该函数在范围[first1,last1)中搜索该序列最后一次出现的地方,并返回一个迭代器指向查找到的第一个元素,如果没有找到则返回 last1。
其中,第一个版本使用 operator== 进行相等性比较,第二个版本使用 pred 进行比较。
仅当目标序列中的所有元素都找到时,才算找到。参见 search.例子:
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; }
-
find_first_of
// equality (1) template <class ForwardIterator1, class ForwardIterator2> ForwardIterator1 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); // predicate (2) template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator1 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
解释:返回一个迭代器,指向 [first1, last1)中第一个与 [first2, last2)中任一个元素匹配的元素;如果没有找到,则返回 last1。
其中,第一个版本使用 operator== 进行相等性比较,第二个版本使用 pred 进行比较。例子:
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; }
-
adjacent_find
// 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);
解释:查找 [first, last) 范围内,第一次出现连续两个元素相等(或令 pred 返回true)的地方,并返回一个迭代器指向第一个匹配的元素;如果没有找到,则返回 last。
例子:
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; }
-
count
template <class InputIterator, class T> typename iterator_traits<InputIterator>::difference_type count(InputIterator first, InputIterator last, const T& val);
解释:返回 [first, last) 范围内与 val 相等(使用 operator==)的元素个数。
例子:
// 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";
-
count_if
template <class InputIterator, class UnaryPredicate> typename iterator_traits<InputIterator>::difference_type count_if(InputIterator first, InputIterator last, UnaryPredicate pred);
解释:返回 [first, last) 范围内令 pred 返回 true 的元素个数。
例子:
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; }
-
mismatch
// 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);
解释:将[first1,last1)范围内的元素与 从first2开始范围内的元素进行比较(使用 operator== 或 pred ),并返回一对迭代器,分别指向两个范围内第一个不匹配的元素。
例子:
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; }
-
equal
// 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);
解释:将[first1,last1)范围内的元素与 从first2开始范围内的元素进行比较,如果两个范围中的所有元素都匹配(使用 operator== 或 pred 进行比较),则返回true。
例子:
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; }
-
search
// 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);
解释:[first2,last2)定义了一个要查找的目标序列,该函数在范围[first1,last1)中搜索该序列第一次出现的地方,并返回一个迭代器指向查找到的第一个元素,如果没有找到则返回 last1。
其中,第一个版本使用 operator== 进行相等性比较,第二个版本使用 pred 进行比较。
仅当目标序列中的所有元素都找到时,才算找到。参见 find_end.例子:
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; }
-
search_n
// 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 );
解释:在[first,last)范围内搜 count 个数元素的序列,且其中每个元素都等于 val(或者 pred 返回true)。如果找到,则返回一个迭代器指向第一个目标元素,否则返回 last.
例子:
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; }