非修改类算法
顾名思义,与修改类算法相比这个类别的算法就是不会对元素的值或者元素之间的位置关系进行改变。这类算法主要就是包括:统计、最大值、最小值、查找等常见的操作。主要的算法与代码示例如下:
操作 | 说明 |
---|---|
count(first,last,value) | 在[first, last)中统计值等于value的元素个数 |
count_if(first,last,p) | 在[first,last)中统计元素符合p操作元素的个数 |
all_of(first,last,p) | 判断[first,last)中的所有元素是否都符合p操作 |
any_of(first,last,p) | 判断[first,last)中的元素是否至少有一个符合p操作 |
none_of(first,last,p) | 判断[first,last)中的所有元素是否都不符合p操作 |
mismatch(first,last,first2) | 以pair类型返回[first,last)与first2开始的范围内符合规则的元素(默认为==,可以自定义并以第4参数调用) |
equal(first,last,first2) | 判断[first,last)与first2开始的范围内元素是否符合规则(默认规则==,可以自定义规则作为第4参数) |
find(first,last,value) | 查找[first,last)内第一个值为value的元素并返回位置,否则返回last |
find_if(first,last,p) | 查找[first,last)内第一个满足p操作的元素并返回位置,否则返回last |
find_if_not(first,last,p) | 查找[first,last)内第一个不满足p操作的元素并返回位置,否则返回last |
find_end(first,last,s_first,s_last) | 查找[first,last)内最后一个为[s_first,s_last)的子集,并返回开始位置否则返回last(默认为==,可以自定义规则作为第5个参数) |
find_first_of(first,last,s_first,s_last) | 查找[first,last)内第一个为[s_first,s_last)的子集,并返回开始位置否则返回last(默认为==,可以自定义规则作为第5个参数) |
adjacent_find(first,last) | 查找第一个相邻相等元素,并返回第一个元素的位置否则返回last(默认为==,可以自定义规则作为第3个参数) |
search(first,last,s_first,s_last) | 查找[first,last-(s_last-s_first))内第一个为[s_first,s_last)的子集,并返回开始位置否则返回last(默认为==,可以自定义规则作为第5个参数,如果s_first==s_last则返回first) |
search_n(first,last,count,value) | 查找[first,last)中长度为count值为value的子集(默认为==,可以自定义规则作为第5个参数) |
???????????????? | 为了表格左侧美观 |
代码示例:
//count and count_if example
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool IsOdd(int i) { return ((i % 2) == 1); }
int main()
{
int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 }; // 8 elements
int mycount = std::count(myints, myints + 8, 10);
cout << "10 appears " << mycount << " times." << endl;
vector<int> myvector{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
mycount = count_if(myvector.begin(), myvector.end(), IsOdd);
cout << "myvector contains " << mycount << " odd values." << endl;
return 0;
}
//output:
//10 appears 3 times.
//myvector contains 5 odd values.
// all_of any_of none_of example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main () {
vector<int> foo{3,5,7,11,13,17,19,23};
if (all_of(foo.begin(), foo.end(), [](int i){return i%2;}))
cout << "All the elements are odd numbers."<<endl;
if (any_of(foo.begin(), foo.end(), [](int i){return i<5;}))
cout << "There is element less than 5 in the range."<<endl;
if (none_of(foo.begin(), foo.end(), [](int i){return i<3;}))
cout << "No element less than 3 in the range."<<endl;
return 0;
}
//output:
//All the elements are odd numbers.
//There is element less than 5 in the range.
//No element less than 3 in the range.
// find find_if find_if_not example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool IsOdd(int i) { return ((i % 2) == 1); }
int main() {
// using find with vector and iterator:
vector<int> myvector1{ 10, 20, 30, 40 };
vector<int>::iterator it;
it = find(myvector1.begin(), myvector1.end(), 30);
if (it != myvector1.end())
cout << "Element found in myvector1: " << *it << endl;
else
cout << "Element not found in myvector1" << endl;
// using find_if with vector and iterator:
vector<int> myvector2{ 1, 2, 3, 4 };
it = find_if(myvector2.begin(), myvector2.end(), IsOdd);
if (it != myvector2.end())
cout << "The first odd value is " << *it << endl;
else
cout << "No odd value in myvectoe2" << endl;
// using find_if_not with vector and iterator:
it = find_if_not(myvector2.begin(), myvector2.end(), IsOdd);
if (it != myvector2.end())
cout << "The first even value is " << *it << endl;
else
cout << "No even value in myvectoe2" << endl;
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。