容器常用函数、algorithm头文件

2023-11-16

偶然发现有个<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;

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

容器常用函数、algorithm头文件 的相关文章

随机推荐