容器部分就不记录了 容易基本差不多 直接从算法开始记录
非变易算法:不改变数据,只对数据进行操作
- 逐个查找算法:for_each
- 元素搜索算法:find, find_if, adjacent_find, find_first_of
- 元素统计算法:count, count_if
- 序列匹配算法:mismatch, equal
- 子序列搜索算法:search, search_n, find_end
变:http://music.573114.com/Blog/Html/B593/631963.html#001
for_each:
01 #include <iostream>
02 #include <algorithm>
03 #include <vector>
04 using namespace std;
05 void print(int x) {
06 cout << x << " ";
07 }
08 int main(void) {
09 vector<int> v;
10 for(int i = 0; i < 10; i++) {
11 v.push_back(i * 2);
12 }
13 for_each(v.begin(), v.end(), print);
14 return 0;
15 }
find:
01 #include <iostream>
02 #include <algorithm>
03 #include <vector>
04 using namespace std;
05 int main(void) {
06 vector<int> v;
07 for(int i = 0; i < 10; i++) {
08 v.push_back(i * 2);
09 }
10 vector<int>::iterator iv = find(v.begin(), v.end(), 6);
11 if(iv == v.end()) {
12 cout << "Find nothing." << endl;
13 } else {
14 cout << "The postion of " << *iv << " is " << iv - v.begin() << endl;
15 cout << "The previous element of it is " << *(--iv) << endl;
16 }
17 return 0;
18 }
find_if
01 #include <iostream>
02 #include <algorithm>
03 #include <vector>
04 using namespace std;
05 int divBy3(int x) {
06 return x % 3 ? 0 : 1;
07 }
08 int main(void) {
09 vector<int> v;
10 for(int i = 1; i < 10; i++) {
11 v.push_back(i * 2);
12 }
13 vector<int>::iterator iv = find_if(v.begin(), v.end(), divBy3);
14 if(iv == v.end()) {
15 cout << "None could be divided by 3 with no remaineder." << endl;
16 } else {
17 cout << *iv << " could be divided by 3 with no remainder." << endl;
18 }
19 return 0;
20 }