< algorithm >
1.abs()
绝对值函数
2.__gcd(x, y)
求最大公约数
3.reverse(a + 1, a + n + 1)
将数组中的元素反转,string,vector用迭代器
reverse_copy(a,a+10,b)
倒序放入b数组中,string,vector用迭代器
4.unique(a + 1, a + n + 1)
去重函数,去重后数组大小为n=unique(a + 1, a + n + 1)-a;
元素去重。即 ”删除”序列中所有相邻的重复元素(只保留一个)。 此处的删除,并不是真的删除,而 是指重复元素的位置被不重复的元素给占领了。由于它”删除”的是相邻的重复元素,所以在使用unique函数之前,一般都会将目标序列进行排序。
#include<iostream>
#include<algorithm>
#include<array>
using namespace std;
int main()
{
array<int,10> a={1,1,4,4,1,6,7,8,9,0};
unique(a.data(),a.data()+10);
for(int x:a)
cout<<x;
return 0;
}
输出结果:1416789090
5.count(a.begin(),a.end(),x)
统计a中x个数,a可以是vector,string 。 int 数组:count(a,a+n);
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> a;
for(int i=0;i<50;i++)
a.push_back(6);
cout<<count(a.begin(),a.end(),6);
string s;
cin>>s;
cout<<count(s.begin(),s.end(),'3');
}
lower_bound( ) 找到首个大于等于某数的数。upper_bound( ) 找到首个大于某数的数
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int len=5;
int a[len]={23,9,7,2,3,4};
sort(a,a+len);
int *x=lower_bound(a,a+len,7);
//int *x=lower_bound(a,a+len,7);
cout<<"不小于7的下确界元素为"<<*x;
}
next_permutation()将数组变成下一个全排列
#include <iostream>
#include<algorithm>
using namespace std;
int main() {
int a[]={1,2,3,4};
do{
for(int i=0;i<4;i++)
cout<<a[i]<<" ";
cout<<endl;
}while(next_permutation(a,a+4));
return 0;
}
#incldue< string >
menset(a,0,n) 只能初始化为0或-1
s.substr(n,m)输出从下标n开始的m个字符串
sscanf(“s.c_str()”,"%d-%d-%d",&y,&m,&d); 从字符串中读入数值
erase
(1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
(2)erase(position);删除position处的一个字符(position是个string类型的迭代器)
(3)erase(first,last);删除从first到last之间的字符(first和last都是迭代器)
queue
priority_queue q;优先队列
自动迭代器
for(const auto& it:m)
{
cout<<it.first<<" ";
printf("%.4lf%\n",it.second/n*100);
}
结构体<运算符重写
struct S{
int num,grade;
bool operator<(const S &a){
if(grade==a.grade)
return num<a.num;
return grade>a.grade;
}
};