C++sort +重载

一.sort函数

1.sort函数包含在头文件为#include<algorithm>的c++标准库中,调用标准库里的排序方法可以实现对数据的排序,但是sort函数是如何实现的,我们不用考虑!

2.sort函数的模板有三个参数:

void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

(1)第一个参数first:是要排序的数组的起始地址。

(2)第二个参数last:是结束的地址(最后一个数据的后一个数据的地址)

(3)第三个参数comp是排序的方法:可以是从升序也可是降序。如果第三个参数不写,则默认的排序方法是从小到大排序。

3.实例

复制代码

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 main()
 5 {
 6   //sort函数第三个参数采用默认从小到大
 7   int a[]={45,12,34,77,90,11,2,4,5,55};
 8   sort(a,a+10);
 9   for(int i=0;i<10;i++)
10   cout<<a[i]<<" ";     
11 } 

复制代码

运行结果:

复制代码

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 bool cmp(int a,int b);
 5 main(){
 6   //sort函数第三个参数自己定义,实现从大到小 
 7   int a[]={45,12,34,77,90,11,2,4,5,55};
 8   sort(a,a+10,cmp);
 9   for(int i=0;i<10;i++)
10     cout<<a[i]<<" ";     
11 }
12 //自定义函数
13 bool cmp(int a,int b){
14   return a>b;
15 }

复制代码

运行结果:

复制代码

 1 #include<iostream>
 2 #include<algorithm>
 3 #include"cstring"
 4 using namespace std;
 5 typedef struct student{
 6   char name[20];
 7   int math;
 8   int english;
 9 }Student;
10 bool cmp(Student a,Student b);
11 main(){
12   //先按math从小到大排序,math相等,按english从大到小排序 
13   Student a[4]={{"apple",67,89},{"limei",90,56},{"apple",90,99}};
14   sort(a,a+3,cmp);
15   for(int i=0;i<3;i++)    
16   cout<<a[i].name <<" "<<a[i].math <<" "<<a[i].english <<endl;     
17 }
18 bool cmp(Student a,Student b){
19   if(a.math >b.math )
20   return a.math <b.math ;//按math从小到大排序 
21   else if(a.math ==b.math )
22       return a.english>b.english ; //math相等,按endlish从大到小排序23 
24 }

复制代码

运行结果

4.对于容器,容器中的数据类型可以多样化

  1) 元素自身包含了比较关系,如int,double等基础类型,可以直接进行比较greater<int>() 递减, less<int>() 递增(省略)

复制代码

 1 #include<iostream>
 2 #include<algorithm>
 3 #include"vector"
 4 using namespace std;
 5 typedef struct student{
 6     char  name[20];
 7     int math;
 8     int english;
 9 }Student;
10 bool cmp(Student a,Student b);
11 main(){
12     int s[]={34,56,11,23,45};
13     vector<int>arr(s,s+5);
14     sort(arr.begin(),arr.end(),greater<int>());
15     for(int i=0;i<arr.size();i++)
16         cout<<arr[i]<<" ";        
17 }

复制代码

运行结果:

2)元素本身为class或者struct,类内部需要重载< 运算符,实现元素的比较;

 注意事项:bool operator<(const className & rhs) const;  如何参数为引用,需要加const,这样临时变量可以赋值;重载operator<为常成员函数,可以被常变量调用; 

 

复制代码

 1 #include<iostream>
 2 #include<algorithm>
 3 #include"vector"
 4 using namespace std;
 5 typedef struct student{
 6     char  name[20];
 7     int math;
 8     //按math从大到小排序 
 9     inline bool operator < (const student &x) const {
10         return math>x.math ;
11     }
12 }Student;
13 main(){
14     Student a[4]={{"apple",67},{"limei",90},{"apple",90}};
15     sort(a,a+3);
16     for(int i=0;i<3;i++)    
17         cout<<a[i].name <<" "<<a[i].math <<" " <<endl;     
18 }

复制代码

运行结果:

重载<也可以定义为如下格式:

 

1 struct Cmp{
2     bool operator()(Info a1,Info a2) const {
3     return a1.val > a2.val;
4     }
5 };
### 前置说明 在 C++ 中,`++` 是一种一元运算符,可以用于变量的自增操作。它可以分为 **前缀形式** 和 **后缀形式** 两种用法。通过运算符重载技术,开发者可以在类中定义 `++` 的行为。 以下是关于如何在 C++ 编程中重载 `++` 运算符的具体方法及其示例代码: --- ### 自增运算符重载的核心概念 #### 前缀形式 (`++obj`) - 表达式的值是在执行加一之后的结果。 - 返回的是修改后的对象本身。 #### 后缀形式 (`obj++`) - 表达式的值是在执行加一之前的原始值。 - 需要返回一个临时的对象来表示未改变的状态。 为了区分这两种情况,在声明和定义时会使用额外的参数——通常是无意义的整型参数列表 `(int)` 来标记这是后缀版本[^1]。 --- ### 示例代码展示 下面是一个完整的例子,演示了如何在一个简单的类中实现 `++` 运算符的重载: ```cpp #include <iostream> using namespace std; class Counter { private: int value; public: // 构造函数初始化计数器 Counter(int v = 0) : value(v) {} // 前缀 ++ 实现 Counter& operator++() { ++value; // 对成员变量进行增量操作 return *this; // 返回当前对象引用 } // 后缀 ++ 实现 (注意多了一个 int 参数) Counter operator++(int) { Counter temp = *this; // 创建并保存旧状态副本 ++(*this); // 调用前缀版完成实际增加逻辑 return temp; // 返回之前保存的老数据作为结果 } void showValue() const { cout << "Current Value: " << value << endl; } }; // 测试程序部分 int main(){ Counter c1(5); cout << "Before Prefix Increment:" ; c1.showValue(); ++c1; // 使用前缀++ cout << "\nAfter Prefix Increment:\n"; c1.showValue(); cout << "\nBefore Postfix Increment:"; c1.showValue(); c1++; // 使用后缀++ cout << "\nAfter Postfix Increment:\n"; c1.showValue(); return 0; } ``` 上述代码展示了如何分别处理前后缀的情况,并且每种情况下都提供了相应的解释[^2]。 --- ### 关键点解析 1. **前缀 vs 后缀** - 当用户调用 `++counter` 或者 `counter++` 时,编译器能够依据语法结构自动匹配到对应的重载函数[^3]。 2. **为什么后缀需要传入 int?** - 定义上区别开两者的关键在于签名差异;由于无法单纯靠名字区分同名不同语义的操作符,所以约定俗成地给后者附加了个假参表`(int)`以便于识别. 3. **效率考量** - 如果只关心性能而不考虑兼容性的话,则可能更倾向于仅提供前缀版本即可满足大部分需求场景下的应用。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值