STL 之sort使用

博客介绍了C++中sort模板的两种形式,第一种按operate<排列元素,第二种用自定义函数代替。还给出多个使用sort的代码示例,包括对整数、结构体等的排序。此外,说明了在成员函数中使用sort时比较算子的注意事项及原因,还提出了static位置含义的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sort模板有两种:
---------------------------------------------------------------------
template<classRanIt>
voidsort(RanItfist,RanItlast);
template<classRanIt,classPred>
voidsort(RanItfist,RanItlast,Predpr);
---------------------------------------------------------------------
第一种模板,sort重排[first,last)之间的元素,产生一个按operate<排列的序列。sort将序列中的元素以升序方式排列。
第二种模板和前一个的行为相似,不过它用pr(X,Y)代替了operate<(x,y)。

例子:
------------------------------
//tmp1.cpp:Definestheentrypointfortheconsoleapplication.
//

#include"stdafx.h"
#include<vector>
#include<algorithm>//Includealgorithms
#include<iostream>

usingnamespacestd;

boolpr(ints1,ints2)
{
returns1>s2;
}

intmain(intargc,char*argv[])
{
vector<int>vec;
vector<int>::iteratori;

vec.push_back(10);
vec.push_back(3);
vec.push_back(7);
sort(vec.begin(),vec.end(),pr);//Sortthevector

for(i=vec.begin();i!=vec.end();i++)
{
cout<<*i<<endl;
}

return0;
}

例子2:

--------------------------------------------------------------------------------------

// tmp1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <vector>
#include <algorithm> // Include algorithms
#include <iostream>
#include <string>

using namespace std;

class myless
{

public:
bool operator()( const int &a, const int &b) {
return a < b;
}
};

int main(int argc, char* argv[])
{
vector<int> vec;
vector<int>::iterator i;

vec.push_back (10);
vec.push_back (3);
vec.push_back (7);
sort(vec.begin(), vec.end(), myless()); // Sort the vector

for (i = vec.begin(); i != vec.end(); i++)
{
cout<<*i<<endl;
}

return 0;
}

例子3:

------------------------------------------------------------------------

// tmp1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <vector>
#include <algorithm> // Include algorithms
#include <iostream>
#include <string>

using namespace std;

typedef struct
{
string first;
string last;
}NAME;

bool sortbyfirst(const NAME& n1, const NAME& n2)
{
return (n1.first<n2.first);
}

bool sortbylast(const NAME& n1, const NAME& n2)
{
return (n1.last<n2.last);
}

int main(int argc, char* argv[])
{
vector<NAME> contacts;
vector<NAME>::iterator j;

NAME tmp;
tmp.first = "liu";
tmp.last = "bei";
contacts.push_back(tmp);

tmp.first = "zhao";
tmp.last = "yun";
contacts.push_back(tmp);

tmp.first = "gun";
tmp.last = "yu";
contacts.push_back(tmp);

tmp.first = "zhang";
tmp.last = "fei";
contacts.push_back(tmp);

cout<<"by first:"<<endl;

sort(contacts.begin(), contacts.end(), sortbyfirst);

for(j=contacts.begin(); j!= contacts.end(); j++)
{
cout<<j->first<<" "<<j->last<<endl;
}

cout<<"by last:"<<endl;

sort(contacts.begin(), contacts.end(), sortbylast);

for(j=contacts.begin(); j!= contacts.end(); j++)
{
cout<<j->first<<" "<<j->last<<endl;
}
return 0;
}

另外还需要说明一下,如果std::sort在一个成员函数中使用的话,

那么定义的比较算子(Predpr)不能是此类的成员函数,而应该是一个全局的函数。

比如,

struct node{

int i;

int j;

string str;

}

class A{

.....

public:

void use_sort();

bool ALess(node n1,node2)

{

if(n1.i<n2.i) return true;

else return false;

}

......

}

bool BLess(node n1,node n2)

{

if(n1.i<n2.i) return true;

else return false;

}

void A::use_sort()

{

vector<node>node_vec;

.....

sort(node_vec.begin(),node_vec.end(),ALess);//not work

sort(node_vec.begin(),node_vec.end(),BLess);//work

}

但是原因是什么呢?

原因其实很简单,因为成员函数只能通过.或者->才能调用的,只写一个函数名进行没有办法为调用。

如果把ALess定义为static , 如

bool static ALess(node n1,node2)

{

if(n1.i<n2.i) return true;

else return false;

}

......

}

就可以了,又有个问题,static放在bool的前面和放在bool的后面有没有什么区别,有区别的话分别是什么含义呢?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值