sort使用方便,效率较高,底层实现是用快排的方式进行排序
要使用该算法,需要#include 头文件(算法头文件)
sort底层实现用的是快速排序
对基本类型的数组从小到大排序:
sort(数组名+n1,数组名+n2);
n1和n2都是int类型的表达式,可以包含变量
如果n1=0,则 + n1可以不写
将数组中下标范围为[n1,n2)的元素从小到大排序。下标为n2的元素不在排序 区间内
程序样例:对无序数组进行递增排序
using namespace std;
int main(){
int a[10]={12,23,423,2,1,53,65,10,32,45};
sort(a, a+10);
for(int i=0; i<10; i++){
cout << a[i] << " ";
}
return 0;
}
结果:1 2 10 12 23 32 45 53 65 423
对元素类型为T的基本类型数组从大到小排序:
sort(数组名+n1,数组名+n2,greater());
**greater()**是从大到小排序规则,<>中的T是数据类型,例如int,float,double等等
泛型概念:
①.泛型:就是不使用具体数据类型,而是使用一种通用类型 T 来进行程序设计;T 只是一个占位符,实际在 T 的位置真实的数据类型取决于用户的需求;占位符的替换由编译器在编译阶段完成。
②.泛型编程:为了避免因数据类型的不同,而被迫重复编写大量相同业务逻辑的代码,因此发展了泛型及泛型编程技术;泛型编程就是独立于任何特定类型的方式编写代码,常用到STL容器、迭代器、和算法都是泛型编程的例子。
程序样例:对数组进行从大到小排序
#include
using namespace std;
int main(){
int a[10]={12,23,423,2,1,53,65,10,32,45};
sort(a, a+10,greater());
for(int i=0; i<10; i++){
cout << a[i] << " ";
}
return 0;
}
结果:423 65 53 45 32 23 12 10 2 1
用自定义的排序规则,对任何类型T的数组排序
sort(数组名+n1,数组名+n2,排序规则结构名());
排序规则结构的定义方式:
struct 结构名{
bool operator()(const T & a1, const T & a2){
//若a1应该在a2前面,则返回true, 否则返回false
}
};
程序样例:
#include
#include
#include
using namespace std;
struct Student {
char name[20]; //姓名
int id; //学号
double gpa;//绩点
};
Student students [] = { {"Jack",112,3.4},{"Mary",102,3.8},
{"Mary",117,3.9}, {"Ala",333,3.5},
{"Zero",101,4.0}
};
struct StudentRule1 { //按姓名从小到大排
bool operator() (const Student & s1,const Student & s2) {
if( stricmp(s1.name,s2.name) < 0)
return true; return false;
}
};
struct StudentRule2 { //按id从小到大排
bool operator() (const Student & s1,const Student & s2) {
return s1.id < s2.id;
}
};
struct StudentRule3 {//按gpa从高到低排
bool operator() (const Student & s1,const Student & s2) {
return s1.gpa > s2.gpa;
}
};
void PrintStudents(Student s[],int size){
for(int i = 0;i < size;++i)
cout << "(" << s[i].name << "," << s[i].id <<"," << s[i].gpa <<")";
cout << endl;
}
int main() {
int n = sizeof(students) / sizeof(Student);
sort(students,students+n,StudentRule1()); //按姓名从小到大排
PrintStudents(students,n);
sort(students,students+n,StudentRule2()); //按id从小到大排
PrintStudents(students,n);
sort(students,students+n,StudentRule3()); //按gpa从高到低排
PrintStudents(students,n);
return 0;
结果:
(Ala,333,3.5) (Jack,112,3.4) (Mary,102,3.8) (Mary,117,3.9) (Zero,101,4)
(Zero,101,4) (Mary,102,3.8) (Jack,112,3.4) (Mary,117,3.9) (Ala,333,3.5)
(Zero,101,4) (Mary,117,3.9) (Mary,102,3.8) (Ala,333,3.5) (Jack,112,3.4)
}
题目描述:
某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前5名学生发奖学金。期末,每个学生都有3门课的成绩:语文、数学、英语。先按总分从高到低排序,如果两个同学总分相同,再按语文成绩从高到低排序,如果两个同学总分和语文成绩都相同,那么规定学号小的同学 排在前面,这样,每个学生的排序是唯一确定的。
任务:先根据输入的3门课的成绩计算总分,然后按上述规则排序,最后按排名顺序输出前五名名学生的学号和总分。注意,在前5名同学中,每个人的奖学金都不相同,因此,你必须严格按上述规则排序。例如,在某个正确答案中,如果前两行的输出数据(每行输出两个数:学号、总分) 是:
7 279
5 279
这两行数据的含义是:总分最高的两个同学的学号依次是77号、55号。这两名同学的总分都是 279279 (总分等于输入的语文、数学、英语三科成绩之和) ,但学号为77的学生语文成绩更高一些。如果你的前两名的输出数据是:
5 279
7 279
则按输出错误处理,不能得分。
输出格式:
共5行,每行是两个用空格隔开的正整数,依次表示前55名学生的学号和总分
输入输出格式:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Wg5po9u0-1623129092555)(http://m.qpic.cn/psc?/V13qV41h30gptO/45NBuzDIW489QBoVep5mcdSSPdHLxT03MIsr2i3.xoQSuqn29Rr6KaYMkEoAA7qW28Ih2KLtKKwaXwZjyeeznRfgZoqpp*1YSeZgH9zkaQc!/b&bo=1wMnAgAAAAABF8E!&rf=viewer_4)]
代码实现:
#include
using namespace std;
struct Students {
int id; // 学号
int c; // 语文成绩
int m; // 数学成绩
int e; // 英语成绩
int sum;
} S[305];
struct cmp {
bool operator()(const Students & s1, const Students & s2) {
if(s1.sum < s2.sum) { // 总成绩高的排在前面
return 0;
} else if(s1.sum > s2.sum) {
return 1;
} else { // 总成绩相等,按语文成绩排名
if(s1.c > s2.c) { // 语文成绩相等,学号小的排在前面
return 1;
} else if(s1.c < s2.c) {
return 0;
} else {
if(s1.id > s2.id)
return 0;
else
return 1;
}
}
}
};
int main() {
int n;
cin >> n;
for(int i=1; i<=n; i++) {
S[i].id = i; // 学号
cin >> S[i].c >> S[i].m >> S[i].e;
S[i].sum = S[i].c + S[i].m + S[i].e;
}
sort(S+1, S+1+n, cmp());
for(int i=1; i<=5; i++) {
cout << S[i].id << " " << S[i].sum << endl;
}
return 0;
}
使用string类要包含头文件 #include
string对象的初始化:
– string s1(“Hello”);
– string month = “March”; //常用方法
– string s2(8,’x’);//初始化8个x字符
可以将字符赋值给string对象
– string s;
– s = ‘n’;
程序样例:
#include
using namespace std;
int main(){
string s1("hello worle");
cout << s1 << endl;
string s2(8,'x');
cout << s2 << endl;
string month = "March";
cout << month << endl;
string s;
s = 'n';
cout << s << endl;
return 0;
}
运行结果:
hello worle
xxxxxxxx
March
n
#include
using namespace std;
int main(){
string str = "hello world";
cout << str.length();
return 0;
}
运行结果:11
s2[5] = s1[3] = ‘a’;
string s1(“hello”);
for(int i=0; i cout << s1.at(i) << endl; //可以改成s1[i] 数组的方式使用 } 成员函数 at() 会做范围检查,如果超过范围,会抛出 out_of_range 异常,而下标运算符 [] 不会做范围检查。 用 + 运算符连接字符串 string s1("good "), s2(“morring!”); s1 += s2; cout << s1; //结果是:good morring! 用成员函数 append() 连接字符串 string s1("good "), s2(“morring!”); s1.append(s2); cout << s1; //结果是:good morring! s2.append(s1, 3, s1.size()); //s1.size()和s1.length()的功能一样 cout << s2; //下标为3开始,s1.size()个字符,如果字符串没有足够字符,则复制到字符串最后一个字符 //结果是:morring!d 。 用关系运算符比较 string 的大小:== , > , >= , < , <= , != 返回值是bool 类型,成立返回 true,否则返回 false 例如: string s1(“hello”), s2(“hello”), s3(“hell”); bool b = (s1 == s2); //结果:1 b = (s1 == s3); //结果:0 b = (s1 > s3); //结果:1 用成员函数 compare() 比较 string 的大小 string s1(“hello”), s2(“hello”), s3(“hell”); int f1 = s1.compare(s2); //结果:0 hello == hello int f2 = s1.compare(s3); //结果:1 hello > hell int f3 = s3.compare(s1); //结果:-1 hell < hello int f4
3. 比较string