sort( )
sort ( ) 排序函数,使用需加上algorithm,三个参数:
比qsort( )简单好用.
#include <iostream>
#include <algorithm>
using namespace std;
sort(首元素的地址(必填),尾元素地址的下一个地址(必填),比较函数(非必填));
比较函数默认递增排序.
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int a[10] = {2,1,3,5,4,7,8,6,9,0};
//a[0]~a[5]递增排序
sort(a, a+6);
for(int i = 0; i < 6; i++)
cout<<a[i];
cout<<endl;
sort(a, a+10);
for(int i = 0; i < 10; i++)
cout<<a[i];
cout<<endl;
return 0;
}
123457
0123456789
double
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
double a[5] = {3.4, 2.5, -1.2, 0.0, 1};
//a[0]~a[5]递增排序
sort(a, a+5);
for(int i = 0; i < 5; i++)
cout<<a[i]<<' ';
cout<<endl;
return 0;
}
字符数组排序(默认字典顺序)
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
char a[10] = {'j','i','n','g','h','u','i'};
//a[0]~a[6]递增排序
sort(a, a+7);
for(int i = 0; i < 7; i++)
cout<<a[i]<<' ';
cout<<endl;
return 0;
}
g h i i j n u
制定排序规则compare函数
sort( ) 默认从小到大排序.
#include <iostream>
#include <algorithm>
using namespace std;
bool cmp(int a, int b) {
return a > b;
}
int main() {
int a[10] = {1, 2, 4, 5, 7, 9, 0};
//a[0]~a[6]递减
sort(a, a+7, cmp);
for(int i = 0; i < 7; i++)
cout<<a[i]<<' ';
cout<<endl;
return 0;
}
9 7 5 4 2 1 0
char,double 类似
结构体数组排序
#include <iostream>
#include <algorithm>
using namespace std;
struct node {
int x, y;
}ssd[10];
bool cmp(node a, node b) {
return a.x > b.x;
}
int main() {
ssd[0].x = 2;
ssd[0].y = 2;
ssd[1] = {3, 3};
ssd[2] = {4, 3};
ssd[3] = {5, 6};
ssd[4] = {6, 1};
sort(ssd, ssd+2, cmp);
for (int i = 0; i < 5; i++)
cout<<ssd[i].x<<' '<<ssd[i].y<<endl;
cout<<endl;
sort(ssd+2, ssd+5, cmp);
for (int i = 0; i < 5; i++)
cout<<ssd[i].x<<' '<<ssd[i].y<<endl;
cout<<endl;
return 0;
}
结构体二级排序,当x大小相等的时候,按照y的大小排序.
bool cmp(node a, node b) {
if (a.x != b.x) return a.x > b.x;
else return a.y > b.y;
#include <iostream>
#include <algorithm>
using namespace std;
struct node {
int x, y;
}ssd[10], *p;
bool cmp(node a, node b) {
if (a.x != b.x) return a.x > b.x;
else return a.y > b.y;
}
int main() {
ssd[0].x = 2;
ssd[0].y = 2;
ssd[1] = {3, 3};
ssd[2] = {3, 5};
ssd[3] = {3, 6};
ssd[4] = {6, 1};
sort(ssd, ssd+5, cmp);
for (int i = 0; i < 5; i++)
cout<<ssd[i].x<<' '<<ssd[i].y<<endl;
cout<<endl;
return 0;
}
6 1
3 6
3 5
3 3
2 2
容器排序
STL标准容器中,sort( ) 支持vector, string, deque.
vector
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool cmp (int a, int b) {
return a > b;
}
int main() {
vector<int> vi;
vi.push_back(3);
vi.push_back(1);
vi.push_back(2);
sort(vi.begin(), vi.end(), cmp);
for (int i = 0; i < vi.size(); i++)
cout<<vi[i]<<' ';
return 0;
}
string
string: size() = length()最后一个元素的索引;
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string str[4] = {"ddd","ccc","aaa","bbb"};
sort(str, str+4);
for (int i = 0; i <= str->size(); i++)
cout<<str[i]<<' ';
return 0;
}
aaa bbb ccc ddd
按字符串长度排序:
bool cmp(string str1, string str2){
return str1.length() > str2.length();
}
lower_bound() upper_bound()
作用在一个有序的数组或容器上,接受三个参数(first, last, val)
first, last 是地址范围,last : 结束地址的下一个地址
lower_bound() :返回数组或容器中第一个>=val元素的位置/指针;(数组:指针,容器:迭代器)
upper_bound():返回第一个值>val元素的位置.
如果,数组或容器中没有找到对应的元素,返回可以插入val的位置(指针)时间复杂度Olog(last - first)
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
int a[10] = {1, 2, 2 , 3, 3, 3, 5,5,5,5};
int * lowerP = lower_bound(a, a+10, 3);
int * upperP = upper_bound(a, a+10, 3);
cout<<lowerP - a<<endl;
cout<<upperP - a<<endl;
return 0;
}
3
6