1 字符和整数排序
#include <iostream>
#include <algorithm>
using namespace std;
void stl1()
{
int a[]={-1,9,-34,100,45,2,98,32};
int len=sizeof(a)/sizeof(int);
sort(a,a+len);//由小到大排列
sort(a,a+len,greater<int>()); //由大到小排列
}
void stl2()
{
char a[]="zasdasdkjhaskjnasdjh";
int len=sizeof(a)/sizeof(char);
sort(a,a+len,greater<char>()); //由大到小排列
sort(a,a+len);
}
2 结构体排序
//结构体排序
struct Node
{
int x,y;
}p[1001];
int cmp(Node a,Node b)
{
if (a.x!=b.x)
{
return a.x<b.x;
}
return a.y<b.y;
}
void stl3()
{
int n;
n=3;
for (int i=0;i<n;i++)
{
cin>>p[i].x>>p[i].y;
}
sort(p,p+n,cmp);
//cout<<"OK";
for (int i=0;i<n;i++)
{
cout<<p[i].x<<" "<<p[i].y<<endl;
}
}
3 学生成绩求和,排序
#include <iostream>
#include <algorithm>
using namespace std;
struct studen1
{
char ID[10];
char name[20];
int maths1;
int chinese1;
int english1;
int count1;
};
int cmpstu(studen1 a11,studen1 b11)
{
return a11.count1<b11.count1;
}
void fun123()
{
struct studen1 stu1[3];
/*
struct studen1 stu1[3]={{"1001","jack",90,80,70},
{"1002","tom",100,88,71},
{"1003","rose",88,66,0}
};
*/
freopen("studen1.in","r",stdin);
freopen("studen1.out","w",stdout);
for (int i=0;i<3;i++)
{
cin>>stu1[i].ID>>stu1[i].name>>stu1[i].maths1>>stu1[i].chinese1>>stu1[i].english1;
stu1[i].count1=stu1[i].chinese1+stu1[i].english1+stu1[i].maths1;
}
sort(stu1,stu1+3,cmpstu);//由小到大排序
for (int i=0;i<3;i++)
{
cout<<stu1[i].ID<<" "<<stu1[i].name<<" "<<stu1[i].count1<<endl;
}
fclose(stdout);
fclose(stdin);
}
4 单词排序
void stl4()
{
//单词排序
freopen("WordSort.in","r",stdin);
string words[100];
int n,num=0;
cin>>n;
for (int i=0;i<n;i++)
{
cin>>words[num];
bool has=false;
for (int i=0;i<num;i++)
{
/*
str1.compare(str2);
compare为比较字符串 如果相等则输出为0,不等则输出为-1。
*/
if(words[i].compare(words[num])==0)
{
has=true;
break;
}
}
if (!has)
{
num++;
}
}
sort(words,words+num);
for (int i=0; i<num; i++)
{
cout << words[i] << endl;
}
fclose(stdin);
}
void stl4_1()
{
//单词排序
freopen("WordSort.in","r",stdin);
int n;
string a[100];
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
for(int i=0;i<n;i++)
{
if(a[i]==a[i+1])
{
continue;
}
cout<<a[i]<<endl;
}
}
5 志愿者选拔
struct data
{
int num,sc;
} a[5005];
bool cmp1(data a,data b)
{
if((a.sc>b.sc)||(a.sc==b.sc&&a.num<b.num))
return 1;
else return 0;
}
void stl5()
{
//志愿者选拔
int n,m,r=0;
freopen("voluntary4.in","r",stdin);
cin>>n>>m;
for(int i=0; i<n; i++)
{
cin>>a[i].num>>a[i].sc;
}
sort(a,a+n,cmp1);
int s=a[int(m*1.5)].sc;
for(int i=0; i<n; i++)
{
if(a[i].sc>=s)
{
r++;
}
}
cout<<s<<' '<<r<<endl;
for(int i=0; i<r; i++)
{
cout<<a[i].num<<' '<<a[i].sc<<endl;
}
fclose(stdin);
}
6 奖学金
#define N 300
struct node2
{
int num;
int Ch;
int Ma;
int En;
int sum;
} a1[N];
int cmp2(node2 a,node2 b)
{
if(a.sum==b.sum)
{
if(a.Ch==b.Ch)
return a.num<b.num;
return a.Ch>b.Ch;
}
return a.sum>b.sum;
}
void stl6()
{
//奖学金
freopen("scholar.in","r",stdin);
int n;
cin>>n;
for(int i=0; i<n; i++)
{
a1[i].num=i;
cin>>a1[i].Ch>>a1[i].Ma>>a1[i].En;
a1[i].sum=a1[i].Ch+a1[i].Ma+a1[i].En;
}
sort(a1,a1+n,cmp2);
for(int i=0; i<5; i++)
{
cout<<a1[i].num<<" ";
cout<<a1[i].sum<<endl;
}
fclose(stdin);
}
lower_bound 和 upper_bound
#include <iostream>
#include <algorithm>
using namespace std;
/*
//在 [first, last) 区域内查找不小于 val 的元素
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
const T& val);
//在 [first, last) 区域内查找第一个不符合 comp 规则的元素
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
const T& val, Compare comp);
//查找[first, last)区域中第一个大于 val 的元素。
ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,
const T& val);
//查找[first, last)区域中第一个不符合 comp 规则的元素
ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,
const T& val, Compare comp);
*/
void stl11()
{
//升序数组
int a[]={1,1,2,2,3,3,3,4,4,4};
//lower_bound()用于寻找第一个大于等于val的位置
int i=lower_bound(a,a+10,0)-a;//0
cout<<i<<endl;
i=lower_bound(a,a+10,1)-a;//0
cout<<i<<endl;
i=lower_bound(a,a+10,3)-a;//4
cout<<i<<endl;
i=lower_bound(a,a+10,5)-a;//10
cout<<i<<endl;
//upper_bound()用于寻找第一个大于val的位置
int j=upper_bound(a,a+10,0)-a;//0
cout<<j<<endl;
j=upper_bound(a,a+10,1)-a;//2
cout<<j<<endl;
j=upper_bound(a,a+10,3)-a;//7
cout<<j<<endl;
j=upper_bound(a,a+10,4)-a;//10
cout<<j<<endl;
j=upper_bound(a,a+10,5)-a;//10
cout<<j<<endl;
}
void stl12()
{
//降序数组使用 lower_bound upper_bound
//lower_bound(first,last,val,greater<int>()),
//若val在序列中,则返回val第一次出现的位置,
//否则返回第一个插入val,不影响原序列顺序的位置。
int a[]={4,4,3,3,2,2,1,1};
int i=lower_bound(a,a+8,0,greater<int>())-a; //8
cout<<i<<endl;
i=lower_bound(a,a+8,4,greater<int>())-a;//0
cout<<i<<endl;
i=lower_bound(a,a+8,1,greater<int>())-a;//6
cout<<i<<endl;
i=lower_bound(a,a+8,3,greater<int>())-a;//2
cout<<i<<endl;
i=lower_bound(a,a+8,5,greater<int>())-a;//0
cout<<i<<endl;
//upper_bound(first,last,val,greater<int>())
//val在序列中,返回第一个小于val的位置,
//否则 返回第一个插入val不影响原序列顺序的位置
int j=upper_bound(a,a+8,0,greater<int>())-a;//8
cout<<j<<endl;
j=upper_bound(a,a+8,4,greater<int>())-a;//2
cout<<j<<endl;
j=upper_bound(a,a+8,1,greater<int>())-a;//8
cout<<j<<endl;
j=upper_bound(a,a+8,3,greater<int>())-a;//4
cout<<j<<endl;
j=upper_bound(a,a+8,5,greater<int>())-a;//0
cout<<j<<endl;
}