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 = ; i < ; ++ i)
iVec.push_back(i); cout << endl;
typedef vector<int> IVEC; //std::find
IVEC::const_iterator iter = std::find(iVec.begin(), iVec.end(), );
if(iVec.end() != iter)
cout << endl << "The value is " << *iter << endl;
else
cout << endl << "Can not find the value " << << endl; //std::accumulate
int nSum = std::accumulate(iVec.begin(), iVec.end(), );
cout << endl << "The sum is " << nSum << endl; cout << endl; //fill
vector<int> iVec2();
std::fill(iVec2.begin(), iVec2.end(), );
for(IVEC::const_iterator iter = iVec2.begin(); iter != iVec2.end(); ++ iter)
cout << *iter << ", "; cout << endl; //fill_n
vector<int> iVec3();
std::fill_n(back_inserter(iVec3), , );
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 = ; i < ; ++ 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();
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 = ; i < ; ++ i)
lst2.push_back(i * );
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(), , );
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(), , );
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 = ; i < ; ++ 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 ; } //stable_sort 降序排列
bool IsBigger(const int& nFirst, const int& nSecond)
{
return nFirst > nSecond;
} //是偶数
bool IsOushu(const int& nNum)
{
return ( == nNum % );
}
执行结果