【STL】帮你复习STL泛型算法 一

 

 

STL泛型算法

 

#include <iostream> #include <vector> #include <algorithm> #include <iterator> #include <numeric> #include <list>
using std::cout; using std::endl; using std::vector; using std::list; bool  IsOushu(const int& nNum); bool IsBigger(const int& nFirst, const int& nSecond); int main() { vector<int> iVec; for(int i = 0; i < 10; ++ i) iVec.push_back(i); cout << endl; typedef vector<int> IVEC; //std::find
    IVEC::const_iterator iter = std::find(iVec.begin(), iVec.end(), 5); if(iVec.end() != iter) cout << endl << "The value is " << *iter << endl; else cout << endl << "Can not find the value " << 5 << endl; //std::accumulate
    int nSum = std::accumulate(iVec.begin(), iVec.end(), 100); cout << endl << "The sum is " << nSum << endl; cout << endl; //fill
    vector<int> iVec2(20); std::fill(iVec2.begin(), iVec2.end(), 100); for(IVEC::const_iterator iter = iVec2.begin(); iter != iVec2.end(); ++ iter) cout << *iter << ", "; cout << endl; //fill_n
    vector<int> iVec3(5); std::fill_n(back_inserter(iVec3), 10, 100); cout << endl << "size of iVec3 is " << iVec3.size() << endl; for(IVEC::const_iterator iter = iVec3.begin(); iter != iVec3.end(); ++ iter) cout << *iter << ", "; cout << endl; cout << endl; //copy
    vector<int> iVec4; list<int> lst1; for(int i = 0; i < 10; ++ i) lst1.push_back(i); std::copy(lst1.begin(), lst1.end(), back_inserter(iVec4)); for(IVEC::const_iterator iter = iVec4.begin(); iter != iVec4.end(); ++ iter) cout << *iter << ", "; cout << endl << endl; //copy
    vector<int> iVec5(11); std::copy(lst1.begin(), lst1.end(), iVec5.begin()); for(IVEC::const_iterator iter = iVec5.begin(); iter != iVec5.end(); ++ iter) cout << *iter << ", "; cout << endl << endl; //replace
    list<int> lst2; for(int i = 0; i < 10; ++ i) lst2.push_back(i * 2); cout << endl; //打印replace之前到值
    cout << endl << "打印lst2 replace之前到值 " << endl; for(list<int>::const_iterator iter = lst2.begin(); iter != lst2.end(); ++ iter) cout << *iter << ", "; cout << endl; cout << "打印replace之后到值 " << endl; std::replace(lst2.begin(), lst2.end(), 8, 888); for(list<int>::const_iterator iter = lst2.begin(); iter != lst2.end(); ++ iter) cout << *iter << ", "; cout << endl; cout << endl; //replace_copy
   list<int> lst3(lst2.size()); std::replace_copy(lst2.begin(), lst2.end(), lst3.begin(), 888, 999); cout << endl << "打印lst2 replace_copy 之后 lst3 到值 " << endl; for(list<int>::const_iterator iter = lst3.begin(); iter != lst3.end(); ++ iter) cout << *iter << ", "; cout << endl; //stable_sort
   vector<int> iVec6; for(int i = 0; i < 10; ++ i) iVec6.push_back(i); cout << endl; cout << endl << "打印stable_sort之前到iVec6到值 " << endl; for(IVEC::const_iterator iter = iVec6.begin(); iter != iVec6.end(); ++ iter) cout << *iter << ", "; cout << endl << "打印stable_sort之后到iVec6到值 " << endl; std::stable_sort(iVec6.begin(), iVec6.end(), IsBigger); for(IVEC::const_iterator iter = iVec6.begin(); iter != iVec6.end(); ++ iter) cout << *iter << ", "; cout << endl << endl; //count_if
   cout << endl << "计算iVec6中偶数到个数 " << endl; int nNums = std::count_if(iVec6.begin(), iVec6.end(), IsOushu); cout << endl << "iVec6中偶数个数为 " << nNums <<"" << endl; cout << endl << endl; cout << "\nThis is main function \n"; return 0; } //stable_sort 降序排列
bool IsBigger(const int& nFirst, const int& nSecond) { return nFirst > nSecond; } //是偶数
bool  IsOushu(const int& nNum) { return (0 == nNum % 2); }

 

 

 

 

执行结果

 

转载于:https://my.oschina.net/u/926461/blog/350871

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值