1、sort
sort(iter1,iter2,comp);
//第三个元素是用于排序的函数,返回值必须是bool类型的值,函数的参数必须和需要排序的容器的里面的基本数据类型一样(默认为基本数据类型自带的 < )
2、reverse
//将某一个容器倒序排列,和sort用法相同
reverse(iter1,iter2);
3、swap(T&a, T&b)
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
typedef struct _name
{
string first;
string second;
_name(string a, string b):first(a), second(b) {};
}Name;
int main()
{
Name bob("Andrew", "Bob");
Name alice("Hilter", "Alice");
swap(bob, alice);
cout << bob.second << endl;
return 0;
}
3、stable_sort
//和sort用法一样,但是sort改变地址,stable_sort()不改变地址
4、partial_sort(iter1, iter1 + n, iter2)
排序最小的n个数
5、is_sorted()
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> vec = {1, 2, 2, 3, 2};
bool a = is_sorted(vec.begin(), vec.end() - 1);//判断容器是否排好序,两端都取
cout << a << endl;
return 0;
}
6、replace
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> tmp{1, 2, 3, 4, 5, 1, 1, 1};
replace(tmp.begin(), tmp.end(), 1, 6);
//把tmp里的1全换成6
for (auto x : tmp)
{
cout << x << ' ';
}
cout << endl;
return 0;
}
7、remove
remove(iter1,iter2,value);//value是要被删除的值,要全部删掉,但是remove的操作是一种赋值,就是如果it的值为value,就把it的值赋给后面一个,而后面是不去操作的
//eg
vector<int> tmp{1, 1, 1, 2, 3, 4, 1, 5};
remove(tmp.begin(),tmp.end(),1);
//tmp={2,3,4,5,3,4,1,5}
//remove会把不是1的元素都提取到前面去,后面没有操作到的值是不变的。
版权声明:本文为baidu_41689104原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。