偶然发现有个<algorithm>头文件,对于容器操作很有用处,摘要如下:
iset.insert(ivector.begin(),ivector.end()) //复制vector元素到set中,注意这个操作会去重
string str = join(vector," "); //vector拼接成一个string
The header <algorithm> definesa collection of functions especially designed to be used on rangesof elements.
<algorithm> 包括常用的find、count、search、replace、reserve、partition、基于sorting的函数、基于比较的函数(max/min等)、堆函数(Heap)
find函数:#include<algorithm>
InputIterator find( InputIterator first, InputIterator last, constT& value );
copy函数:#include <algorithm>
OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result );
vector resolving_res; //利用字符串流把string切分为vector
istringstream iss(outanswer);
copy(istream_iterator(iss),istream_iterator(), back_inserter(resolving_res));
int myints[]={10,20,30,40,50,60,70}; //利用copy函数copy数组to vector
vector<int> myvector;
vector<int>::iterator it;
myvector.resize(7); // allocate space for 7elements, Resizes the vector to contain 7 elements.
copy ( myints,myints+7,myvector.begin() );
似乎下面这个也可以完成copy功能:
int myints[] ={10,20,30,5,15};
vector<int> v(myints,myints+5);
replace函数:#include<algorithm>
void replace ( ForwardIterator first,ForwardIterator last, const T& old_value, constT& new_value );
int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20};
vector<int> myvector (myints,myints+8); // 10 20 30 30 20 10 10 20
replace (myvector.begin(), myvector.end(), 20, 99); // 10 99 3030 99 10 10 99
count函数:#include<algorithm>
count(oriPos.begin(),oriPos.end(),"mm")>1 //count适用于vector,统计vector中有多少个某元素
注意count在set中只返回0或者1;