c++ stl算法
程序员文章站
2022-07-12 16:30:59
...
这里是一些stl常用算法,搭配迭代器、function object、lambda有强大的功能
#include<array>
#include<vector>
#include<deque>
#include<list>
#include<forward_list>
#include<map>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<algorithm>
#include<iterator>
#include<functional>
#include<numeric>
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
using namespace placeholders;
template <typename T>
inline void INSERT_ELEMENTS(T& coll,int first,int last){
for(int i=first;i<=last;++i){
coll.insert(coll.end(),i);
}
}
template <typename T>
inline void PRINT_ELEMENTS(const T& coll,const std::string& optcstr=""){
cout<<optcstr;
for(auto elem:coll){
cout<<elem<<' ';
}
cout<<endl;
}
template <typename T>
inline void PRINT_MAPPED_ELEMENTS(const T& coll,const string& optcstr=""){
cout<<optcstr;
for(auto elem:coll){
cout<<'['<<elem.first<<','<<elem.second<<"] ";
}
cout<<endl;
}
template<typename T>
class AddVal{
private:
T theVal;
public:
AddVal(const T& v):theVal(v){}
void operator()(T& elem){
elem+=theVal;
}
};
class MeanVal{
private:
long num,sum;
public:
MeanVal():num(0),sum(0){}
void operator()(int elem){
num++;
sum+=elem;
}
operator double(){
return static_cast<double>(sum)/static_cast<double>(num);
}
};
int main(){
//for_each
// vector<int>coll;
// INSERT_ELEMENTS(coll,1,8);
/* for_each(coll.begin(),coll.end(),[](int elem){cout<<elem<<' ';});
cout<<endl;
for_each(coll.begin(),coll.end(),[](int& elem){elem+=10;
});
PRINT_ELEMENTS(coll);
for_each(coll.begin(),coll.end(),[=](int& elem){elem+=*coll.begin();
});
for_each(coll.begin(),coll.end(),AddVal<int>(10)); */
// transform(coll.begin(),coll.end(),coll.begin(),[](int elem){return elem+10;});
// transform(coll.begin(),coll.end(),coll.begin(),[=](int elem){return *coll.begin()+elem;});
// PRINT_ELEMENTS(coll);
// double mv=for_each(coll.begin(),coll.end(),MeanVal());
// cout<<"mean val: "<<mv<<endl;
//count
/* PRINT_ELEMENTS(coll);
auto num = count(coll.begin(),coll.end(),4);
cout<<"number of elements equal to 4: "<<num<<endl;
num=count_if(coll.begin(),coll.end(),[](int elem){return elem%2==0;
});
cout<<"number of elements with even value: "<<num<<endl;
num = count_if(coll.begin(),coll.end(),[](int elem){return elem>4;
});
cout<<"number of elements greater than 4: "<<num<<endl; */
//find
/* list<int>coll;
INSERT_ELEMENTS(coll,1,9);
INSERT_ELEMENTS(coll,1,9);
PRINT_ELEMENTS(coll,"coll: ");
auto pos1=find(coll.begin(),coll.end(),4);
decltype(pos1)pos2;
if(pos1!=coll.end())
pos2=find(++pos1,coll.end(),4);
if(pos1!=coll.end()&&pos2!=coll.end()){
copy(--pos1,++pos2,ostream_iterator<int>(cout," "));
cout<<endl;
}
vector<int>coll;
INSERT_ELEMENTS(coll,1,9);
PRINT_ELEMENTS(coll,"coll: ");
auto pos=find_if(coll.begin(),coll.end(),bind(greater<int>(),_1,3));
cout<<"the "<<distance(coll.begin(),pos)+1<<".element is the first greater than 3"<<endl;
pos=find_if(coll.begin(),coll.end(),[](int elem){
return elem%3==0;});
cout<<"the "<<distance(coll.begin(),pos)+1<<".ele is the first divisible by 3"<<endl;
pos=find_if_not(coll.begin(),coll.end(),bind(less<int>(),_1,5));
cout<<"the first value >= 5:"<<*pos<<endl;
//copy
vector<string>coll1={"hello","this","is","an","sample"};
list<string>coll2;
copy(coll1.begin(),coll1.end(),back_inserter(coll2));
copy(coll2.begin(),coll2.end(),ostream_iterator<string>(cout," "));
cout<<endl;
copy(coll1.begin(),coll1.end(),coll2.begin());
copy(coll2.begin(),coll2.end(),ostream_iterator<string>(cout," "));
cout<<endl;
copy(istream_iterator<string>(cin),istream_iterator<string>(),ostream_iterator<string>(cout," "));
cout<<endl;
//transform
vector<int>coll1;
list<int>coll2;
INSERT_ELEMENTS(coll1,1,9);
PRINT_ELEMENTS(coll1,"coll: ");
// transform(coll1.begin(),coll1.end(),coll1.begin(),negate<int>());
transform(coll1.begin(),coll1.end(),coll1.begin(),coll1.begin(),multiplies<int>());
// PRINT_ELEMENTS(coll1,"negated: ");
PRINT_ELEMENTS(coll1,"squared: ");
// transform(coll1.begin(),coll1.end(),back_inserter(coll2),bind(multiplies<int>(),_1,10));
transform(coll1.begin(),coll1.end(),coll1.rbegin(),back_inserter(coll2),plus<int>());
PRINT_ELEMENTS(coll2,"coll2: ");
cout<<"diff: ";
// transform(coll2.rbegin(),coll2.rend(),ostream_iterator<int>(cout," "),[](int elem){return -elem;
// });
transform(coll1.begin(),coll1.end(),coll2.begin(),ostream_iterator<int>(cout," "),minus<int>());
cout<<endl;
//fill
fill_n(ostream_iterator<float>(cout," "),10,7.7);
cout<<endl;
list<string>coll;
fill_n(back_inserter(coll),9,"hi");
PRINT_ELEMENTS(coll,"coll: ");
fill(coll.begin(),coll.end(),"again");
PRINT_ELEMENTS(coll,"coll: ");
fill_n(coll.begin(),coll.size()-2,"hello");
PRINT_ELEMENTS(coll,"coll: ");
list<string>::iterator pos1,pos2;
pos1=coll.begin();
pos2=coll.end();
fill(++pos1,--pos2,"hmmm");
PRINT_ELEMENTS(coll,"coll: ");
//generate
list<int>coll;
generate_n(back_inserter(coll),5,rand);
PRINT_ELEMENTS(coll);
generate(coll.begin(),coll.end(),rand);
PRINT_ELEMENTS(coll);
//replace
list<int>coll;
INSERT_ELEMENTS(coll,2,6);
INSERT_ELEMENTS(coll,4,9);
PRINT_ELEMENTS(coll,"coll: ");
//replace(coll.begin(),coll.end(),6,42);
replace_copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),5,55);
cout<<endl;
replace_copy_if(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),bind(less<int>(),_1,5),42);
cout<<endl;
replace_copy_if(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),[](int elem){return elem%2==1;
},0);
cout<<endl;
// PRINT_ELEMENTS(coll,"coll: ");
// replace_if(coll.begin(),coll.end(),[](int elem){return elem<5;
// },0);
// PRINT_ELEMENTS(coll,"coll: ");
//remove
vector<int>coll;
INSERT_ELEMENTS(coll,2,6);
INSERT_ELEMENTS(coll,4,9);
INSERT_ELEMENTS(coll,1,7);
PRINT_ELEMENTS(coll,"coll: ");
vector<int>::iterator pos;
pos=remove(coll.begin(),coll.end(),5);
PRINT_ELEMENTS(coll,"size not chang: ");
coll.erase(pos,coll.end());
PRINT_ELEMENTS(coll,"size chang: ");
coll.erase(remove_if(coll.begin(),coll.end(),[](int elem){return elem<4;
}),coll.end());
PRINT_ELEMENTS(coll,"<4 removeed: "); */
list<int>coll;
INSERT_ELEMENTS(coll,1,6);
INSERT_ELEMENTS(coll,1,9);
PRINT_ELEMENTS(coll);
remove_copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),3);
cout<<endl;
remove_copy_if(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),[](int elem){
return elem>4;});
cout<<endl;
multiset<int>coll2;
remove_copy_if(coll.begin(),coll.end(),inserter(coll2,coll2.end()),bind(less<int>(),_1,4));
PRINT_ELEMENTS(coll2);
return 0;
}
推荐阅读