(1)全局函数在容器中加入,不用带参数;
for_each(vec.begin(),vec.end(),Display); //1.全局函数在容器中加入,不用带参数;
(2) 仿函数在容器中加入,不用带参数;
for_each(vec.begin(),vec.end(),CDisplay()); //2.仿函数在容器中加入,不用带参数;
(3)lamabda函数在容器中加入,带参数; //a.lamabda()表达式和仿函数无关
for_each(vec.begin(),vec.end(),[](int(n)){ //3.lamabda函数在容器中加入,带参数; //a.lamabda()表达式和仿函数无关
cout<<n<<"\t";});
#include
#include
#include
#include
#include
#include
using namespace std;
class CDisplay{
public:
void operator()(int n){
cout << n << "\t";
}
};
void Display(int n){
cout << n << "\t";
}
int main()
{
vector<int> vec{1,2,3,4,5,6};
for_each(vec.begin(),vec.end(),Display); //1.全局函数在容器中加入,不用带参数;
cout << endl;
for_each(vec.begin(),vec.end(),CDisplay()); //2.仿函数在容器中加入,不用带参数;
cout << endl;
for_each(vec.begin(),vec.end(),[](int(n)){ //3.lamabda函数在容器中加入,带参数; //a.lamabda()表达式和仿函数无关
cout<<n<<"\t";});
cout << endl;
cout << "----------------------"<<endl;
list<int> li{11,12,13,14,15,16};
for_each(li.begin(),li.end(),Display); //1.全局函数在容器中加入,不用带参数;
cout << endl;
for_each(li.begin(),li.end(),CDisplay()); //2.成员函数在容器中加入,不用带参数;
cout << endl;
cout << "----------------------"<<endl;
set<int> s{31,32,33,34,35,36};
for_each(s.begin(),s.end(),Display); //1.全局函数在容器中加入,不用带参数;
cout << endl;
for_each(s.begin(),s.end(),CDisplay()); //2.成员函数在容器中加入,不用带参数;
cout << endl;
cout << "----------------------"<<endl;
map<int,string> records{
{1,"张三"}, {2,"李四"}, {3,"王五"}
};
for_each(records.begin(),records.end(),[](pair<int,string> p){
cout<<p.first <<"\t"<<p.second<<endl;});
return 0;
}
查找数组,字符串数组的最小值,注意比较对象max(a,b);
cout << max(1.2,1.3)<< endl;
cout << min("abc","abd")<< endl;//查找数组,字符串数组的最小值,注意比较对象max(a,b);
(1)distance获取顺序容器中的最值,注意解引用;
采用迭代器的方式(auto查找)
list<double> l{1.1,1.2,1.3};
auto max_it = max_element(l.begin(),l.end());
cout << distance(l.begin(),max_it) << "\t" << *max_it << endl;
auto min_it = min_element(l.begin(),l.end());
cout << distance(l.begin(),min_it) << "\t" << *min_it << endl;
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
cout << max(1.2,1.3)<< endl;
cout << min("abc","abd")<< endl;//查找数组,字符串数组的最小值,注意比较对象max(a,b);
//获取顺序容器中的最值,注意解引用;
list<double> l{1.1,1.2,1.3};
auto max_it = max_element(l.begin(),l.end());
cout << distance(l.begin(),max_it) << "\t" << *max_it << endl;
auto min_it = min_element(l.begin(),l.end());
cout << distance(l.begin(),min_it) << "\t" << *min_it << endl;
return 0;
}
a.容器对象:vector,deque,默认升序
sort(vec.begin(),vec.end()); //容器对象:vector,deque,默认升序
b.降序算法(lamabda函数只能写排序算法)
sort(vec.begin(),vec.end(),[](int a,int b){ //sort函数里面的lamada函数只能写排序算法;
return a > b ;
});
c. 翻转:迭代器;
reverse(vec.begin(),vec.end()); //翻转:迭代器;
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
vector<int> vec = {1,3,2,9,8};
for_each(vec.begin(),vec.end(),[](int n){
cout << n << "\t" ;
});
cout << endl;
reverse(vec.begin(),vec.end()); //翻转:迭代器;
for_each(vec.begin(),vec.end(),[](int n){
cout << n << "\t" ;
});
cout << endl;
sort(vec.begin(),vec.end()); //容器对象:vector,deque,默认升序
for_each(vec.begin(),vec.end(),[](int n){
cout << n << "\t" ;
});
cout << endl;
sort(vec.begin(),vec.end(),[](int a,int b){ //sort函数里面的lamada函数只能写排序算法;
return a > b ;
});
for_each(vec.begin(),vec.end(),[](int n){ //sort函数里面的amada函数只能写排序算法;
cout << n << "\t" ;
});
cout << endl;
return 0;
}
void reverseString(vector<char>& s) {
reverse(s.begin(),s.end());
}
(1)排序(2)将另一个向量插入到另一个向量的方法,直接用首尾的索引;
vector<int> getLeastNumbers(vector<int>& arr, int k) {
sort(arr.begin(),arr.end()); //(1)排序
return vector<int>(arr.begin(),arr.begin()+k);
//(2)将另一个向量插入到另一个向量的方法,直接用首尾的索引;
}
if(head == NULL) return NULL; //(1).首先对头结点进行判空操作
(2)将链表存入到向量;
if(head == NULL) return NULL; //(1).首先对头结点进行判空操作
while(head!=NULL){ // (2)将链表存入到向量;
vec.push_back(head);
head = head->next;
}
(3)关于类的比较,需要采用(仿函数,lamabda,运算符重载进行比较算法的写法)
sort(vec.begin(),vec.end(),[](ListNode* a, ListNode* b){ //(3)关于类的比较,需要采用(仿函数,lamabda,运算符重载进行比较算法的写法)
return a->val < b->val; m
});
(4)重新连接链表,向量进行连接
ListNode* root = vec[0];
for(int i =1;i <vec.size();++i){ //(4)重新连接链表,向量进行连接
vec[i-1]->next = vec[i];
}
vec[vec.size()-1]->next = NULL;
return root;
ListNode* sortList(ListNode* head) {
vector<ListNode*> vec;
if(head == NULL) return NULL; //(1).首先对头结点进行判空操作
while(head!=NULL){ // (2)将链表存入到向量;
vec.push_back(head);
head = head->next;
}
sort(vec.begin(),vec.end(),[](ListNode* a, ListNode* b){ //(3)关于类的比较,需要采用(仿函数,lamabda,运算符重载进行比较算法的写法)
return a->val < b->val;
});
ListNode* root = vec[0];
for(int i =1;i <vec.size();++i){ //(4)重新连接链表,向量进行连接
vec[i-1]->next = vec[i];
}
vec[vec.size()-1]->next = NULL;
return root;
}
(1)find()返回索引,存在的话,和distance连用,并且解引用;不存在的,则等于尾指针
list<int> li{1,1,2,3,4,7,8};
auto it = find(li.begin(),li.end(),4);
if(it == li.end()){
cout << "NOT FIND" << endl; //b.不存在的,则等于尾指针
}else{
cout << distance(li.begin(),it) << " \t"<< *it << endl;//a.find()返回索引,存在的话,和distance连用,并且解引用;
}
(2)count():返回等于元素个数,count_if():返回符合条件的个数
cout << count(li.begin(),li.end(),1) << endl; //返回个数
cout << count_if(li.begin(),li.end(),[](int n){
return n<3;}) << endl; //返回个数,符合条件的个数
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
list<int> li{1,1,2,3,4,7,8};
auto it = find(li.begin(),li.end(),4);
if(it == li.end()){
cout << "NOT FIND" << endl; //b.不存在的,则等于尾指针
}else{
cout << distance(li.begin(),it) << " \t"<< *it << endl;//a.find()返回索引,存在的话,和distance连用,并且解引用;
}
cout << count(li.begin(),li.end(),1) << endl; //返回个数
cout << count_if(li.begin(),li.end(),[](int n){
return n<3;}) << endl; //返回个数,符合条件的个数
return 0;
}
二分查找
int search(vector<int>& nums, int target) {
auto it = find(nums.begin(),nums.end(),target);
if(it == nums.end()){
return -1;
}else{
return distance(nums.begin(),it);
}
}
int search(vector<int>& nums, int target) {
return count(nums.begin(),nums.end(),target);
}
统计有序矩阵中的负数
(1)二维向量遍历的n参数代表行向量
(2)count_if()
(3)lamabda的应用
for(auto &r:grid){
res += count_if(r.begin(),r.end(),[](int n){
return n < 0;
});
int countNegatives(vector<vector<int>>& grid) {
int res = 0;
for(auto &r:grid){ // (1)二维向量遍历的n参数代表行向量(2)count_if()(3)lamabda的应用
res += count_if(r.begin(),r.end(),[](int n){
return n < 0;
});
}
return res;
}
(1)头文件
#include //a.头文件
(2)a. accumulate()注意初始值;
b. 可以实现字符串的连接
cout << accumulate(order.begin(),order.end(),0) << endl; //b.注意初始值;
vector<string> s ={"abc","def","hij"};
cout << accumulate(s.begin(),s.end(),string()) << endl; //b.字符串连接
(3) transform:参数1,2: 数据源的容器; 3,目的容器; 4.lamabda操作
transform(order.begin(),order.end(),res.begin(),[](int n){
return n+10;}); //参数1,2: 数据源的容器; 3,目的容器; 4.lamabda操作
#include
#include
#include
#include
#include
#include
#include //a.头文件
using namespace std;
int main()
{
vector<int> order{2,3,7,8,10};
cout << accumulate(order.begin(),order.end(),0) << endl; //b.注意初始值;
vector<int> res(order.size());
transform(order.begin(),order.end(),res.begin(),[](int n){
return n+10;}); //参数1,2: 数据源的容器; 3,目的容器; 4.lamabda操作
cout << accumulate(res.begin(),res.end(),0)<< endl; //
return 0;
}
for(int i = 1; i<=n;++i){
ostringstream oss;
oss << i;
s.push_back(oss.str()); //a.首先将数组转化为字符串
}
(2).对于字符串进行排序
sort(s.begin(),s.end());//b.对于字符串进行排序
(3)//c.最后将字符串转化为数组;方法1;直接调用流函数
transform(s.begin(),s.end(),res.begin(),[](string& s){return stoi(s);});//c.最后将字符串转化为数组;
方法2:进行字符串转化为数组
transform(s.begin(),s.end(),res.begin(),[](string& s){
istringstream iss(s);
int val;
iss >> val;
return val
});//c.最后将字符串转化为数组;
vector<int> lexicalOrder(int n) {
vector<string> s;
for(int i = 1; i<=n;++i){
ostringstream oss;
oss << i;
s.push_back(oss.str()); //a.首先将数组转化为字符串
}
sort(s.begin(),s.end());//b.对于字符串进行排序
vector<int> res(n);
transform(s.begin(),s.end(),res.begin(),[](string& s){return stoi(s);});//c.最后将字符串转化为数组;
return res;
}
0-n-1缺乏的数字
int missingNumber(vector<int>& nums) {
int sum = (nums.size())*(nums.size()+1)/2;
return sum-accumulate(nums.begin(),nums.end(),0);
}
(1) binary_search()
a.优点:快速 ;
b.缺点:只能返回有没有,不能返回index;
vector<int> order{1,2,3,7,8,10};
cout << binary_search(order.begin(),order.end(),7) << endl; //只能返回有没有,不能返回index;
(2)equal_range()
a. 优点:返回的是一个区间,第一个出现和最后一个出现,左闭右开,所以第二个元素需要减1;
第一个的index为p.first,最后一个为p.second-1;
b. 和distance()连用,用于定位;
pair<vector<int>::iterator,vector<int>::iterator> p = equal_range(order.begin(),order.end(),8) ;
if(p.first == p.second){
cout << "NOT FOUND" << endl;
}else{
cout << distance(order.begin(),p.first); //第一个的index为p.first,最后一个为p.second-1;
}
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
vector<int> order{1,2,3,7,8,10};
cout << binary_search(order.begin(),order.end(),7) << endl; //只能返回有没有,不能返回index;
pair<vector<int>::iterator,vector<int>::iterator> p = equal_range(order.begin(),order.end(),8) ;
if(p.first == p.second){
cout << "NOT FOUND" << endl;
}else{
cout << distance(order.begin(),p.first);//第一个的index为p.first,最后一个为p.second-1;
}
return 0;
}
在排序数组中查找元素的第一个和最后一个位置
(1)equal_range()
a。优点:返回的是一个区间,第一个出现和最后一个出现,左闭右开,所以第二个元素需要减1;
第一个的index为p.first,最后一个为p.second-1;
b.和distance()连用,用于定位;
vector<int> searchRange(vector<int>& nums, int target) {
auto p = equal_range(nums.begin(),nums.end(),target);
if(p.first == p.second){
return vector{-1,-1};
}else{
return vector<int>{distance(nums.begin(),p.first),distance(nums.begin(),p.second-1)};
//a。优点:返回的是一个区间,第一个出现和最后一个出现,左闭右开,所以第二个元素需要减1;第一个的index为p.first,最后一个为p.second-1;
//b.和distance()连用,用于定位;
}
}
二分查找
equal_range()和distance()连用
int search(vector<int>& nums, int target) {
auto p = equal_range(nums.begin(),nums.end(),target);
if(p.first == p.second){
return -1;
}else{
return distance(nums.begin(),p.first);a。优点:返回的是一个区间,第一个出现和最后一个出现,左闭右开,所以第二个元素需要减1;第一个的index为p.first,最后一个为p.second-1;
//b.和distance()连用,用于定位;
}
}
(1)copy()
问题:vector vec;注意如果不设置内存大小进行拷贝,会出现空间不足,吐核;
解决之道1:back_inserter(vec) 自行匹配
vector<int> vec; //1.1注意如果不设置内存大小进行拷贝,会出现空间不足,吐核;
copy(l.begin(),l.end(),back_inserter(vec)); //1.解决之道1:自行匹配
解决之道2:明确大小;第三个参数是头结点
vector<int> vec(5); //解决方案2:明确大小;
copy(l.begin(),l.end(),vec.begin()); //1.正序的拷贝,第三个参数是头结点
(2)copy_backward()逆序的拷贝,第三个节点是尾节点
copy_backward(l.begin(),l.end(),vec.end()); //2.逆序的拷贝,第三个节点是尾节点
(1) 问题:返回的是迭代器,并非是真正的删除,需要把返回指针的后面部分去掉;返回有效的范围,
解决之道1:resize()用来返回正确的范围
l.resize(distance(l.begin(),it)); //3.1 resize()用来返回正确的范围
解决之道2:erase()用来去除错误的范围
l.erase(it,l.end());//3.2 earse()用来删除错误的范围;
参数1:首
参数2;尾
参数3:源
参数4:目标
replace(l.begin(),l.end(),5,10); //5.进行替换;
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
/*******copy***********/
list<int> l{1,2,3,4,5};
//vector vec; //1.1注意如果不设置内存大小进行拷贝,会出现空间不足,吐核;
//copy(l.begin(),l.end(),back_inserter(vec)); //1.解决之道1:自行匹配
vector<int> vec(5); //解决方案2:明确大小;
copy(l.begin(),l.end(),vec.begin()); //1.正序的拷贝,第三个参数是头结点
for_each(vec.begin(),vec.end(),[](int n){
cout << n << "\t";});
cout << endl;
/*******incopy***********/
//vector vec1(5);
copy_backward(l.begin(),l.end(),vec.end()); //2.逆序的拷贝,第三个节点是尾节点
for_each(vec.begin(),vec.end(),[](int n){
cout << n << "\t";});
cout << endl;
/********remove**********/
auto it = remove(l.begin(),l.end(),3); //3.移除的问题:返回的是迭代器,并非是真正的删除,需要把返回指针的后面部分去掉;返回有效的范围,
//l.resize(distance(l.begin(),it)); //3.1 resize()用来返回正确的范围;
l.erase(it,l.end());//3.2 earse()用来删除错误的范围;
for_each(l.begin(),l.end(),[](int n){
cout << n << "\t";});
cout << endl;
/********remove_if**********/
auto it1 = remove_if(l.begin(),l.end(),[](int m){return m<3;}); //4.加入条件的移除,返回的是迭代器,并非是真正的删除,需要把返回指针的后面部分去掉;返回有效的范围,
l.resize(distance(l.begin(),it1));//resize()用来返回正确的范围;
for_each(l.begin(),l.end(),[](int n){
cout << n << "\t";});
cout << endl;
/********replace**********/
replace(l.begin(),l.end(),5,10); //5.进行替换;
for_each(l.begin(),l.end(),[](int n){
cout << n << "\t";});
cout << endl;
return 0;
}
头文件 | 功能 |
---|---|
算法函数 | |
数值算法 | |
函数对象/仿函数 |
No. | 分类 | 说明 | - |
---|---|---|---|
1 | 非可变序列算法 | Non-modifying sequence operations | 不直接修改容器内容的算法。 |
2 | 可变序列算法 | Modifying sequence operations | 可以修改容器内容的算法。 |
3 | 排序算法 | Sorting/Partitions/Binary search/ | 对序列排序、合并、搜索算法操作。 |
4 | 数值算法 | Merge/Heap/Min/max对容器内容进行数值计算。 |
函数 | 作用 |
---|---|
fill(beg,end,val) | 将值val赋给[beg,end)范围内的所有元素。 |
fill_n(beg,n,val) | 将值val赋给[beg,beg+n)范围内的所有元素。 |
generate(beg,end,func) | 连续调用函数func填充[beg,end)范围内的所有元素。 |
generate_n(beg,n,func) | 连续调用函数func填充[beg,beg+n)范围内的所有元素。 |
函数 | 作用 |
---|---|
for_each(beg,end,func) | 将[beg,end)范围内所有元素依次调用函数func,返回func。不修改序列中的元素。 |
transform(beg,end,res,unary) | 将[beg,end)范围内所有元素依次调用函数unary,结果放入res中。 |
transform(beg2,end1,beg2,res,binary) | 将[beg,end)范围内所有元素与[beg2,beg2+end-beg)中所有元素依次调用函数unary,结果放入res中。 |
函数 | 作用 |
---|---|
max(a,b) | 返回两个元素中较大一个。 |
max(a,b,cmp) | 使用自定义比较操作cmp,返回两个元素中较大一个。 |
max_element(beg,end) | 返回一个ForwardIterator,指出[beg,end)中最大的元素。 |
max_element(beg,end,cmp) | 使用自定义比较操作cmp,返回一个ForwardIterator,指出[beg,end)中最大的元素。 |
min(a,b) | 返回两个元素中较小一个。 |
min(a,b,cmp) | 使用自定义比较操作cmp,返回两个元素中较小一个。 |
min_element(beg,end) | 返回一个ForwardIterator,指出[beg,end)中最小的元素。 |
min_element(beg,end,cmp) | 使用自定义比较操作cmp,返回一个ForwardIterator,指出[beg,end)中最小的元素。 |
函数 | 作用 |
---|---|
sort(beg,end) | 默认升序重新排列元素 |
sort(beg,end,comp) | 使用函数comp代替比较操作符执行sort()。 |
partition(beg,end,pred) | 元素重新排序,使用pred函数,把结果为true的元素放在结果为false的元素之前。 |
stable_sort(beg,end) | 与sort()类似,保留相等元素之间的顺序关系。 |
stable_sort(beg,end,pred) | 使用函数pred代替比较操作符执行stable_sort()。 |
stable_partition(beg,end) | 与partition()类似,保留容器中的相对顺序。 |
stable_partition(beg,end,pred) | 使用函数pred代替比较操作符执行stable_partition()。 |
partial_sort(beg,mid,end) | 部分排序,被排序元素个数放到[beg,end)内。 |
partial_sort(beg,mid,end,comp) | 使用函数comp代替比较操作符执行partial_sort()。 |
partial_sort_copy(beg1,end1,beg2,end2) | 与partial_sort()类似,只是将[beg1,end1)排序的序列复制到[beg2,end2)。 |
partial_sort_copy(beg1,end1,beg2,end2,comp) | 使用函数comp代替比较操作符执行partial_sort_copy()。 |
nth_element(beg,nth,end) | 单个元素序列重新排序,使所有小于第n个元素的元素都出现在它前面,而大于它的都出现在后面。 |
nth_element(beg,nth,end,comp) | 使用函数comp代替比较操作符执行nth_element()。 |
函数 | 作用 |
---|---|
reverse(beg,end) | 元素重新反序排序。 |
reverse_copy(beg,end,res) | 与reverse()类似,结果写入res。 |
rotate(beg,mid,end) | 元素移到容器末尾,由mid成为容器第一个元素。 |
rotate_copy(beg,mid,end,res) | 与rotate()类似,结果写入res |
函数 | 作用 |
---|---|
random_shuffle(beg,end) | 元素随机调整次序。 |
random_shuffle(beg,end,gen) | 使用函数gen代替随机生成函数执行random_shuffle()。 |
函数 | 作用 |
---|---|
count(beg,end,val) | 利用==操作符,对[beg,end)的元素与val进行比较,返回相等元素个数。 |
count_if(beg,end,pred) | 使用函数pred代替==操作符执行count()。 |
函数 | 作用 |
---|---|
find(beg,end,val) | 利用==操作符,对[beg,end)的元素与val进行比较。当匹配时结束搜索,返回该元素的InputIterator。 |
find_if(beg,end,pred) | 使用函数pred代替==操作符执行find()。find_first_of(beg1,end1,beg2,end2) |
find_first_of(beg1,end1,beg2,end2,pred) | 使用函数pred代替==操作符执行find_first_of()。返回该元素的Iterator。 |
find_end(beg1,end1,beg2,end2) | 在[beg1,end1)范围内查找[beg2,end2)最后一次出现。找到则返回最后一对的第一个ForwardIterator,否则返回end1。 |
find_end(beg1,end1,beg2,end2,pred) | 使用函数pred代替==操作符执行find_end()。返回该元素的Iterator。 |
adjacent_find(beg,end) | 对[beg,end)的元素,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的ForwardIterator。否则返回end。 |
adjacent_find(beg,end,pred) | 使用函数pred代替==操作符执行adjacent_find()。 |
函数 | 作用 |
---|---|
search(beg1,end1,beg2,end2) | 在[beg1,end1)范围内查找[beg2,end2)首一次出现,返回一个ForwardIterator,查找成功,返回[beg1,end1)内第一次出现[beg2,end2)的位置,查找失败指向end1。 |
search(beg1,end1,beg2,end2,pred) | 使用函数pred代替==操作符执行search()。search_n(beg,end,n,val) |
search_n(beg,end,n,val,pred) | 使用函数pred代替==操作符执行search_n()。 |
binary_search(beg,end,val) | 在[beg,end)中查找val,找到返回true。 |
binary_search(beg,end,val,comp) | 使用函数comp代替比较操作符执行binary_search()。 |
函数 | 作用 |
---|---|
lower_bound(beg,end,val) | 在[beg,end)范围内的可以插入val而不破坏容器顺序的第一个位置,返回一个ForwardIterator。 |
lower_bound(beg,end,val,comp) | 使用函数comp代替比较操作符执行lower_bound()。 |
upper_bound(beg,end,val) | 在[beg,end)范围内插入val而不破坏容器顺序的最后一个位置,该位置标志一个大于val的值,返回一个ForwardIterator。 |
upper_bound(beg,end,val,comp) | 使用函数comp代替比较操作符执行upper_bound()。 |
equal_range(beg,end,val) | 返回一对iterator,第一个表示lower_bound,第二个表示upper_bound。 |
equal_range(beg,end,val,comp) | 使用函数comp代替比较操作符执行lower_bound()。 |
函数 | 作用 |
---|---|
copy(beg,end,res) | 复制[beg,end)到res |
copy_backward(beg,end,res) | 与copy()相同,不过元素是以相反顺序被拷贝。 |
函数 | 作用 |
---|---|
remove(beg,end,val) | 删除[beg,end)内所有等于val的元素。注意,该函数不是真正删除函数。 |
remove_if(beg,end,pred) | 删除[beg,end)内pred结果为true的元素。 |
remove_copy(beg,end,res,val) | 将所有不等于val元素复制到res,返回OutputIterator指向被拷贝的末元素的下一个位置。 |
remove_copy_if(beg,end,res,pred) | 将所有使pred结果为true的元素拷贝到res。 |
函数 | 作用 |
---|---|
replace(beg,end,oval,nval) | 将[beg,end)内所有等于oval的元素都用nval代替。 |
replace_copy(beg,end,res,oval,nval) | 与replace()类似,不过将结果写入res。 |
replace_if(beg,end,pred,nval) | 将[beg,end)内所有pred为true的元素用nval代替。replace_copy_if(beg,end,res,pred,nval) |
函数 | 作用 |
---|---|
unique(beg,end) | 清除序列中相邻重复元素,不能真正删除元素。重载版本使用自定义比较操作。 |
unique(beg,end,pred) | 将所有使pred结果为true的相邻重复元素去重。 |
unique_copy(beg,end,res) | 与unique类似,不过把结果输出到res。 |
unique_copy(beg,end,res,pred) | 与unique类似,不过把结果输出到res。 |
函数 | 作用 |
---|---|
swap(a,b) | 交换存储在a与b中的值。 |
swap_range(beg1,end1,beg2) | 将[beg1,end1)内的元素[beg2,beg2+beg1-end1)元素值进行交换。 |
iter_swap(it_a,it_b) | 交换两个ForwardIterator的值。 |
函数 | 作用 |
---|---|
accumulate(beg,end,val) | 对[beg,end)内元素之和,加到初始值val上。accumulate(beg,end,val,binary) |
partial_sum(beg,end,res) | 将[beg,end)内该位置前所有元素之和放进res中。partial_sum(beg,end,res,binary) |
adjacent_difference(beg1,end1,res) | 将[beg,end)内每个新值代表当前元素与上一个元素的差放进res中。 |
adjacent_difference(beg1,end1,res,binary) | 将函数binary代替减法运算,执行adjacent_difference()。 |
inner_product(beg1,end1,beg2,val) | 对两个序列做内积(对应元素相乘,再求和)并将内积加到初始值val上。 |
inner_product(beg1,end1,beg2,val,binary1,binary2) | 将函数binary1代替加法运算,将binary2代替乘法运算,执行inner_product()。 |
函数 | 作用 |
---|---|
equal(beg1,end1,beg2) | 判断[beg1,end1)与[beg2,end2)内元素都相等 |
equal(beg1,end1,beg2,pred) | 使用pred函数代替默认的==操作符。includes(beg1,end1,beg2,end2) |
includes(beg1,end1,beg2,end2,comp) | 将函数comp代替<操作符,执行includes()。 |
lexicographical_compare(beg1,end1,beg2,end2) | 按字典序判断[beg1,end1)是否小于[beg2,end2) |
lexicographical_compare(beg1,end1,beg2,end2,comp) | 将函数comp代替<操作符,执行lexicographical_compare()。 |
mismatch(beg1,end1,beg2) | 并行比较[beg1,end1)与[beg2,end2),指出第一个不匹配的位置,返回一对iterator,标志第一个不匹配元素位置。如果都匹配,返回每个容器的end。 |
mismatch(beg1,end1,beg2,pred) | 使用pred函数代替默认的==操作符。 |
函数 | 作用 |
---|---|
merge(beg1,end1,beg2,end2,res) | 合并[beg1,end1)与[beg2,end2)存放到res。 |
merge(beg1,end1,beg2,end2,res,comp) | 将函数comp代替<操作符,执行merge()。 |
inplace_merge(beg,mid,end) | 合并[beg,mid)与[mid,end),结果覆盖[beg,end)。 |
inplace_merge(beg,mid,end,cmp) | 将函数comp代替<操作符,执行inplace_merge()。 |
set_union(beg1,end1,beg2,end2,res) | 取[beg1,end1)与[beg2,end2)元素并集存放到res。 |
set_union(beg1,end1,beg2,end2,res,comp) | 将函数comp代替<操作符,执行set_union()。 |
set_intersection(beg1,end1,beg2,end2,res) | 取[beg1,end1)与[beg2,end2)元素交集存放到res。 |
set_intersection(beg1,end1,beg2,end2,res,comp) | 将函数comp代替<操作符,执行set_intersection()。 |
set_difference(beg1,end1,beg2,end2,res) | 取[beg1,end1)与[beg2,end2)元素内差集存放到res。 |
set_difference(beg1,end1,beg2,end2,res,comp) | 将函数comp代替<操作符,执行set_difference()。set_symmetric_difference(beg1,end1,beg2,end2,res) |
函数 | 作用 |
---|---|
next_permutation(beg,end) | 取出[beg,end)内的下移一个排列。 |
next_permutation(beg,end,comp) | 将函数comp代替<操作符,执行next_permutation()。 |
prev_permutation(beg,end) | 取出[beg,end)内的上移一个排列。 |
prev_permutation(beg,end,comp) | 将函数comp代替<操作符,执行prev_permutation()。 |
函数 | 作用 |
---|---|
make_heap(beg,end) | 把[beg,end)内的元素生成一个堆。 |
make_heap(beg,end,comp) | 将函数comp代替<操作符,执行make_heap()。 |
pop_heap(beg,end) | 重新排序堆。它把first和last-1交换,然后重新生成一个堆。可使用容器的back来访问被"弹出"的元素或者使用pop_back进行真正的删除。并不真正把最大元素从堆中弹出。 |
pop_heap(beg,end,comp) | 将函数comp代替<操作符,执行pop_heap()。 |
push_heap(beg,end) | 假设first到last-1是一个有效堆,要被加入到堆的元素存放在位置last-1,重新生成堆。在指向该函数前,必须先把元素插入容器后。 |
push_heap(beg,end,comp) | 将函数comp代替<操作符,执行push_heap()。 |
sort_heap(beg,end) | 对[beg,end)内的序列重新排序。 |
sort_heap(beg,end,comp) | 将函数comp代替<操作符,执行push_heap()。 |