目录
1.常用排序算法
sort
random_shuffle
merge
reverse
2.常用拷贝和替换算法
copy
replace
replace_if
swap
1.常用排序算法
sort
默认从小到大排序
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(9);
v.push_back(2);
v.push_back(4);
sort(v.begin(),v.end());
for(auto &ele:v)
{
cout << ele << ' ';
}
sort(v.begin(),v.end(),greater<int>());
//降序排列
cout << endl;
for(auto &ele:v)
{
cout << ele << ' ';
}
return 0;
}
random_shuffle
使用的时候要加随机数种子
#include<bits/stdc++.h>
using namespace std;
void print(int v)
{
cout << v << ' ';
}
int main()
{
srand((unsigned int)time(NULL));
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(9);
v.push_back(2);
v.push_back(4);
for_each(v.begin(),v.end(),print);
cout << endl;
random_shuffle(v.begin(),v.end());
for_each(v.begin(),v.end(),print);
return 0;
}
merge
#include<bits/stdc++.h>
using namespace std;
void print(int v)
{
cout << v << ' ';
}
int main()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(9);
v1.push_back(2);
v1.push_back(4);
sort(v1.begin(),v1.end());
vector<int> v2;
for(int i = 2; i <= 6; i += 2)
{
v2.push_back(i);
}
vector<int> v;
v.resize(v1.size() + v2.size());
//必须v1,v2都是升序或者都是降序
merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v.begin());
for_each(v.begin(),v.end(),print);
return 0;
}
reverse
#include<bits/stdc++.h>
using namespace std;
void print(int v)
{
cout << v << ' ';
}
int main()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(9);
v1.push_back(2);
v1.push_back(4);
for_each(v1.begin(),v1.end(),print);
cout << endl;
reverse(v1.begin(),v1.end());
cout << "反转后" << endl;
for_each(v1.begin(),v1.end(),print);
return 0;
}
2.常用拷贝和替换算法
copy
#include<bits/stdc++.h>
using namespace std;
void print(int v)
{
cout << v << ' ';
}
int main()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(9);
v1.push_back(2);
v1.push_back(4);
for_each(v1.begin(),v1.end(),print);
cout << endl;
vector<int> v2;
v2.resize(v1.size());
copy(v1.begin(),v1.end(),v2.begin());
cout << "复制后" << endl;
for_each(v2.begin(),v2.end(),print);
return 0;
}
replace
#include<bits/stdc++.h>
using namespace std;
void print(int v)
{
cout << v << ' ';
}
int main()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(9);
v1.push_back(2);
v1.push_back(4);
for_each(v1.begin(),v1.end(),print);
cout << endl;
cout << "替换后" << endl;
replace(v1.begin(),v1.end(),2,1000);
for_each(v1.begin(),v1.end(),print);
return 0;
}
replace_if
#include<bits/stdc++.h>
using namespace std;
class print
{
public:
void operator()(int v)
{
cout << v << ' ';
}
};
class dayu2
{
public:
bool operator()(int v)
{
return v > 2;
}
};
int main()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(9);
v1.push_back(2);
v1.push_back(4);
for_each(v1.begin(),v1.end(),print());
cout << endl;
cout << "替换后" << endl;
replace_if(v1.begin(),v1.end(),dayu2(),999);
for_each(v1.begin(),v1.end(),print());
return 0;
}
swap
交换的容器要是同种类型
#include<bits/stdc++.h>
using namespace std;
class print
{
public:
void operator()(int v)
{
cout << v << ' ';
}
};
int main()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(9);
v1.push_back(2);
v1.push_back(4);
vector<int> v2;
for(int i = 1; i <= 3; i++)
{
v2.push_back(i);
}
cout << "交换前" << endl;
for_each(v1.begin(),v1.end(),print());
cout << endl;
for_each(v2.begin(),v2.end(),print());
cout << endl << "交换后" << endl;
swap(v1,v2);
for_each(v1.begin(),v1.end(),print());
cout << endl;
for_each(v2.begin(),v2.end(),print());
return 0;
}