移除性算法
remove() 将等于某特定值的元素全部移除
remove_if() 将满足某准则的元素全部移除
remove_copy() 将不等于某特定值的元素全部复制到它处
remove_copy()_if() 将不满足某准则的元素全部复制到它处
unique() 移除毗邻的重复元素
unique_copy() 移除毗邻的重复元素,并复制到它处
变序性算法
reverse() 将元素的次序逆转
reverse_copy() 复制的同时,逆转元素顺序
rotate() 旋转元素次序
rotate_copy() 复制的同时,旋转元素顺序
next_permutation() 得到元素的下一个排列次序
prev_permutation() 得到元素的上一个排列次序
random_shuffle() 将元素的次序次序随机打乱
partition() 改变元素次序,使符合某准则者移到前面
stable_partition() 与partition()相似,但保持符合准则与不符合准则各个元素之间的相对位置
排序算法
sort() 对所有元素排序
stable_sort() 对所有元素排序,并保持相等元素间的相对次序
partial_sort() 排序,直到前n个元素就位
partial_sort_copy() 排序,直到前n个元素就位,结果复制于它处
nth_element() 根据第n个位置进行排序
partition() 改变元素次序,使符合某准则的元素在前面
stable_partition() 与partition()相同,但保持相对位置
make_heap() 将一个区间转换成一个heap
push_heap() 将元素加入一个heap
pop_heap() 从heap移除一个元素
sort_heap() 对heap进行排序,排序后不再是heap了
已序区间算法
binary_search() 判断某区间内是否包含某个元素
includes() 判断某区间内的每一个元素是否都涵盖于另一区间中
lower_bound() 搜索第一个"大于等于给定值"的元素
upper _bound() 搜索第一个"大于给定值"的元素
equal_range() 返回"等于给定值"的所有元素构成的区间
merge() 将两个区间合并
set_union() 求两个区间的并集
set_intersection() 求两个区间的交集
set_difference() 求位于第一区间但不位于第二区间的所有元素,形成一个已序区间
set_symmetric_difference() 找出只出现于两区间之一的所有元素,形成一个已序区间
inplace_merge() 将两个连续的已序区间合并
数值算法
accumulate() 组合所有元素(求总和,求乘积...)
inner_product() 组合两区间内的所有元素
adjacent_difference() 将每个元素和其前一元素组合
partial_sum() 将每个元素和其先前的所有元素组合
#include <iostream>
#include <vector>
#include <algorithm>
#include <list>
#include <numeric>
using namespace std;
void printElement(int n1)
{
cout<<n1<<" ";
}
void add_3(int& n2)
{
n2+=3;
}
int fun(int n3)
{
return n3*2;
}
int fun2(int n1,int n2)
{
return n1+n2;
}
bool fun3(int n3)
{
return n3<4;
}
bool my_greater(int n1,int n2)
{
return n1>n2;
}
int mult(int n1,int n2)
{
return n1*n2;
}
int main()
{
int a[]={1,2,3,4,5};
vector<int> v(a,a+5);
/*for_each(v.begin(),v.end(),printElement);
cout<<endl;*/
//移除性算法
/*remove(v.begin(),v.end(),3);
for_each(v.begin(),v.end(),printElement);
cout<<endl;*///删除逻辑上的
/*v.erase(remove(v.begin(),v.end(),3),v.end());//配合erase删除物理上的
for_each(v.begin(),v.end(),printElement);
cout<<endl;
*/
/*
rotate(v.begin(),v.begin()+2,v.end());//把1和2 放到最后面
for_each(v.begin(),v.end(),printElement);
cout<<endl;
*/
//变序性算法
/* rotate(v.begin(),v.begin()+2,v.end()-1);//把1和2 放到5前面
for_each(v.begin(),v.end(),printElement);
cout<<endl;
//得到元素的下一个拍排列次序
int i=0;
while(next_permutation(vc.begin(),vc.end()))
{
for_each(vc.begin(),vc.end(),printElement);
i+=1;
cout<<"i="<<i<<endl;
}
//排序性算法
sort(v.begin(),v.end());//默认是从小到大
for_each(v.begin(),v.end(),printElement);
cout<<endl;
sort(v.begin(),v.end(),my_greater);//自定义是从大到小
for_each(v.begin(),v.end(),printElement);
cout<<endl;
*/
//lower_bound的使用
vector<int>::const_iterator v1;
v1=lower_bound(v.begin(),v.end(),3);
if (v1!=v.end())
{
//cout<<*v1<<endl;
cout<<v1-v.begin()<<endl;
}
//upper_bound的使用
v1=upper_bound(v.begin(),v.end(),4);
if (v1!=v.end())
{
//cout<<*v1<<endl;
cout<<v1-v.begin()<<endl;
}
/*for_each(v.begin(),v.end(),printElement);
cout<<endl;
*/
//accumulate的使用,累加
int count1=accumulate(v.begin(),v.end(),1);//进行数据累加最后加1
cout<<count1<<endl;//输出16
//accumulate的使用,累乘
int count2=accumulate(v.begin(),v.end(),2,mult);//进行数据累乘最
cout<<count2<<endl;//输出240
return 0;
}
1、lower_bond():搜索第一个或“大于等于给定值”的元素
如果要插入给定值,保持区间有序性,返回第一个可插入的位置。
2、upper_bond():搜索第一个或“大于给定值”的元素
如果要插入给定值,保持区间有序性,返回最后可插入的位置。