1 sort:排序;
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int main(){
vector<int>a={1,3,5,4,2};
vector<string>c={"wangzihen","ludafu","yanjunnan"};
sort(c.begin(),c.end());
sort(a.begin(),a.end());
for(const auto &i:a){
cout<<i<<" ";
}
cout<<endl;
for(const auto &i:c){
cout<<i<<" ";
}
return 0;
}主要是在于对迭代器的理解,auto自动变量
2 pratial_sort:
确保前n个最小的元素以升序排序,其余元素位置不定
partial_sort(a.begin(),a.begin()+3,a.end());
//只确保前3个最小的元素以升序排序,其余元素位置不定。
可以看出4,5并没有排序;
3 .1lower_bound:查找有序序列中第一个不小于给定值的元素。
auto it=lower_bound(a.begin(),a.end(),4);
cout<<*it<<endl;
根据之前我们知道find_if也是查找容器中的函数,那lower_bouend的好处的主要是时间复杂度,它的时间复杂度为O(log n),而find的时间复杂度为O(n);但是find的用处更广,可以用于未排序的容器。
3.2upper_bound:查找有序序列中第一个大于给定值的元素
auto it=upper_bound(a.begin(),a.end(),2);
cout<<*it<<endl;
3.3binary_search:
在有序序列中查找给定值
if(binary_search(c.begin(),c.end(),"yanjunnan")){
cout<<"he is ungly";
}
else{
cout<<"he is handsame";
}
4.1 max,min:返回最大或最小;
b=max(a,b);
b=min(a,b);
for(const auto &i:b){
cout<<i;
}
4.2 max_element,min_element: 在一个范围内查找最大或最小的元素。
auto ma=max_element(c.begin(),c.end());
auto mi=min_element(c.begin(),c.end());
cout<<*ma<<" "<<*mi<<endl;