STL

容器部分就不记录了 容易基本差不多 直接从算法开始记录


非变易算法:不改变数据,只对数据进行操作

  • 逐个查找算法:for_each
  • 元素搜索算法:find, find_if, adjacent_find, find_first_of
  • 元素统计算法:count, count_if
  • 序列匹配算法:mismatch, equal
  • 子序列搜索算法:search, search_n, find_end
非变:http://www.360doc.com/content/14/0319/12/19525_361841142.shtml

变: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	}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值