不修改内容的序列操作:
adjacent_find |
查找两个相邻的等价元素 |
all_of C++11 |
检测在给定范围中是否所有元素都满足给定的条件 |
any_of C++11 |
检测在给定范围中是否存在元素满足给定条件 |
count |
返回值等价于给定值的元素的个数 |
count_if |
返回值满足给定条件的元素的个数 |
equal |
返回两个范围是否相等 |
find |
返回第一个值等价于给定值的元素 |
find_end |
查找范围 A 中与范围 B 等价的子范围最后出现的位置 |
find_first_of |
查找范围 A 中第一个与范围 B 中任一元素等价的元素的位置 |
find_if |
返回第一个值满足给定条件的元素 |
find_if_not C++11 |
返回第一个值不满足给定条件的元素 |
for_each |
对范围中的每个元素调用指定函数 |
mismatch |
返回两个范围中第一个元素不等价的位置 |
none_of C++11 |
检测在给定范围中是否不存在元素满足给定的条件 |
search |
在范围 A 中查找第一个与范围 B 等价的子范围的位置 |
search_n |
在给定范围中查找第一个连续 n 个元素都等价于给定值的子范围的位置 |
adjacent_find
查找两个相邻的等价元素
|
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// adjacent_find example #include #include #include bool myfunction (int i, int j) { return (i==j); } int main () { int myints[] = {5,20,5,30,30,20,10,10,20}; std::vector std::vector // 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; } |
输出:
|
2 |
the first pair of repeated elements are: 30 the second pair of repeated elements are: 10 |
all_of
检测在给定范围中是否所有元素都满足给定的条件
|
2 3 4 5 6 7 8 9 10 11 12 13 |
// all_of example #include #include #include int main () { std::array if ( std::all_of(foo.begin(), foo.end(), [](int i){return i%2;}) ) std::cout << "All the elements are odd numbers.\n"; return 0; } |
输出:
|
|
All the elements are odd numbers. |
any_of
检测在给定范围中是否存在元素满足给定条件
|
2 3 4 5 6 7 8 9 10 11 12 13 |
// any_of example #include #include #include int main () { std::array 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; } |
输出:
|
|
There are negative elements in the range. |
count
返回值等价于给定值的元素的个数
|
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// count algorithm example #include #include #include 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 mycount = std::count (myvector.begin(), myvector.end(), 20); std::cout << "20 appears " << mycount << " times.\n"; return 0; } |
输出:
|
2 |
10 appears 3 times. 20 appears 3 times. |
count_if
返回值满足给定条件的元素的个数
|
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// count_if example #include #include #include bool IsOdd (int i) { return ((i%2)==1); } int main () { std::vector 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; } |
输出:
|
|
myvector contains 5 odd values. |
equal
返回两个范围是否相等
|
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// equal algorithm example #include #include #include |