# cpp的STL算法

## cpp的STL算法

stl算法的分类

计数算法
count,count_if

search,seach_n

equal,mismatch,lexicographical_compare


初始化算法
fill,fill_n,generate,generate_n

for_each,transform

copy,copy_backward

remove,reomve_if,remove_copy,remove_copy_if
unique,unique_copy

replace,replace_if

sort,stab_sort,partial_sort

partion,stable_partition

binary_search,lower_bound,upper_bound


stl算法的应用

计算元素的个数


//
//  main.cpp
//  use_stl_algorithm
//
//  Created by bikang on 16/11/1.
//

#include <iostream>
#include <algorithm>
#include <vector>
#include <list>

using namespace std;

template <typename T>
class PrintClass {
public:
void operator()(const T& elem)const{
cout << elem << " ";
}
};

template <typename T>
bool isEven(const T& number) {
return ((number%2) == 0);
}

template <typename T>
T addData(const T & v1,const T & v2) {
return (v1+v2);
}

//在容器中计算元素个数和查找元素
void tstlalg1();
//查找序列
void tsearch();
//容器初始化fill 偏移范围 fill_n 开始位置，个数，数值
//generate随机初始化
void tfill();
//transform 容器的内容的变换
void ttransform();
//测试复制和删除
//copy,copy_backward,remove,remove_if
//replace
//partition  stable_partition
void tdelCopy();

//sort binary_search unique 排序 查找 去重复
void  tsort();

int main(int argc, const char * argv[]) {
//tstlalg1();
//tsearch();
//tfill();
//ttransform();
//tdelCopy();
tsort();
return 0;
}

void  tsort(){
vector<string> vec1;
vec1.push_back("tom");
vec1.push_back("tim");
vec1.push_back("tim");
vec1.push_back("tam");
vec1.push_back("tam");
vec1.push_back("kim");
vec1.push_back("kim");
for_each(vec1.begin(), vec1.end(), PrintClass<string>());cout << endl;

//排序
sort(vec1.begin(), vec1.end());
for_each(vec1.begin(), vec1.end(), PrintClass<string>());cout << endl;

//查找
bool searchRes = binary_search(vec1.begin(), vec1.end(), "tom");
if(searchRes){
cout << "find ok"<<endl;
}else{
cout << "find faild"<<endl;
}
searchRes = binary_search(vec1.begin(), vec1.end(), "to1m");
if(searchRes){
cout << "find ok"<<endl;
}else{
cout << "find faild"<<endl;
}

//去重复
vector<string>::iterator ite;
ite = unique(vec1.begin(), vec1.end());
for_each(vec1.begin(), vec1.end(), PrintClass<string>());cout << endl;
vec1.erase(ite, vec1.end());
for_each(vec1.begin(), vec1.end(), PrintClass<string>());cout << endl;

//有序插入,优选的最前和最靠后的位置
ite = lower_bound(vec1.begin(), vec1.end(),lowerStr);
vec1.insert(ite, lowerStr);
for_each(vec1.begin(), vec1.end(), PrintClass<string>());cout << endl;

string upperStr = "ss";
ite = upper_bound(vec1.begin(), vec1.end(),upperStr);
vec1.insert(ite, upperStr);
for_each(vec1.begin(), vec1.end(), PrintClass<string>());cout << endl;

}

void tdelCopy(){

list<int> l1;
for(int j =0;j<10;++j){
l1.push_back(j);
}
for_each(l1.begin(), l1.end(), PrintClass<int>());cout << endl;

//copy
vector<int> vec1(l1.size() *2);
vector<int>::iterator ite;
ite = copy(l1.begin(), l1.end(),vec1.begin());
for_each(vec1.begin(), vec1.end(), PrintClass<int>());cout << endl;
copy_backward(l1.begin(), l1.end(), vec1.end());
for_each(vec1.begin(), vec1.end(), PrintClass<int>());cout << endl;

//remove
remove(vec1.begin(), vec1.end(), 0);
for_each(vec1.begin(), vec1.end(), PrintClass<int>());cout << endl;
//remove if

remove_if(vec1.begin(), vec1.end(), [](int i){return i%2==0;});
for_each(vec1.begin(), vec1.end(), PrintClass<int>());cout << endl;

//replace
replace(vec1.begin(), vec1.end(), 1, 21);
for_each(vec1.begin(), vec1.end(), PrintClass<int>());cout << endl;
//replace if
replace_if(vec1.begin(), vec1.end(), [](int i){return i%2==1;}, 55);
for_each(vec1.begin(), vec1.end(), PrintClass<int>());cout << endl;

//partition
partition(l1.begin(), l1.end(),[](int i){return i%2==1;});
for_each(l1.begin(), l1.end(), PrintClass<int>());cout << endl;
//stable_partition
stable_partition(l1.begin(), l1.end(),[](int i){return i%2==1;});
for_each(l1.begin(), l1.end(), PrintClass<int>());cout << endl;

}

void ttransform(){

string str1 = "this is a simple language!";
string str1low;
str1low.resize(str1.size());
transform(str1.begin(), str1.end(), str1low.begin(), (int(*)(int))toupper);
cout << str1low << endl;

//测试加法
vector<int> vec1;
for (int i =0; i<10; ++i) {
vec1.push_back(i);
}

vector<int> vec2;
for (int i =20; i<30; ++i) {
vec2.push_back(i);
}

vector<int> res;
res.resize(10);

//两个容器的内容相加
for_each(vec1.begin(), vec1.end(), PrintClass<int>());cout << endl;
for_each(vec2.begin(), vec2.end(), PrintClass<int>());cout << endl;
for_each(res.begin(), res.end(), PrintClass<int>());cout << endl;

}

void tfill(){
cout << "tfill" << endl;
vector<int> vec1(3);
fill(vec1.begin(), vec1.end(), 9);
for_each(vec1.begin(), vec1.end(), PrintClass<int>());cout << endl;

vec1.resize(6);
fill_n(vec1.begin()+3, 3, 3);
for_each(vec1.begin(), vec1.end(), PrintClass<int>());cout << endl;
vec1.resize(10);
generate(vec1.begin()+6, vec1.end(), rand);
for_each(vec1.begin(), vec1.end(), PrintClass<int>());cout << endl;
vec1.resize(12);
generate_n(vec1.begin()+10, 2, rand);
for_each(vec1.begin(), vec1.end(), PrintClass<int>());cout << endl;

}

void tsearch(){
cout << "tsearch" << endl;
vector<int> vec1;
for (int i =0; i<10; ++i) {
vec1.push_back(i);
}
for_each(vec1.begin(), vec1.end(), PrintClass<int>());cout << endl;

list<int> l1;
for(int j =3;j<5;++j){
l1.push_back(j);
}

//search查找
vector<int>::iterator ite;
ite = search(vec1.begin(), vec1.end(),l1.begin(), l1.end());
if(ite != vec1.end()){
cout << "pos=" << distance(vec1.begin(), ite);
}else{
cout << " search faild";
}

}

void tstlalg1(){
cout << "start" << endl;
vector<int> vec1;
for (int i =0; i<10; ++i) {
vec1.push_back(i);
}
for_each(vec1.begin(), vec1.end(), PrintClass<int>());cout << endl;
//查找 数据数量
long numCount = 0;
int find_data = 3;
numCount = count(vec1.begin(), vec1.end(), (int)find_data);
cout << "numCount=" <<numCount << endl;

//查找 数据数量
size_t npos = count_if(vec1.begin(), vec1.end(), isEven<int>);
cout << "npos=" <<npos << endl;
//闭包查找 数据数量
npos =count_if(vec1.begin(), vec1.end(), [](int i){return i%4==0;});
cout << "npos=" <<npos << endl;

//find查找数据的位置
vector<int>::iterator ite;
ite = find(vec1.begin(), vec1.end(), find_data);
if(ite != vec1.end()){
cout << "find ok " << *ite << endl;
}else{
cout << "find faild" << endl;
}

//find_if查找数据的位置
ite = find_if(vec1.begin(), vec1.end(), [](int i){return i%4==0;});
if(ite != vec1.end()){
cout << "find ok " << *ite << endl;
}else{
cout << "find faild" << endl;
}

}


01-11 9万+

10-14 1989
11-01 577
10-28 1557
05-25 335
04-20
05-10 592
08-18 971
01-05 217
06-03 461
12-07 155
10-28 743
11-17 1万+
07-21 6万+
06-21 10万+
04-21 6344