关于STL排序 [英] about STL sort

查看:52
本文介绍了关于STL排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用算法排序排序指针向量。在STL中像

这个:


"

int compare(vector< string *> :: iterator a,vector< ; string *> :: iterator b){

return(** a)<(** b);

}


sort(iter,iter_end,compare);

"

但是这个不行,我真的不知道为什么。

有谁能告诉我如何正确使用它?

解决方案

Magcialking写道:
< blockquote class =post_quotes>
我想用算法排序排序指针向量。在STL中像

这个:


"

int compare(vector< string *> :: iterator a,vector< ; string *> :: iterator b){

return(** a)<(** b);

}



应该是:


int compare(const string * a,const string * b)

{

返回* a< * b;

}


sort(iter,iter_end,compare);

"


但是这不行,我真的不知道为什么。

有人能告诉我如何正确使用它吗?


Magcialking写道:


我想用算法排序排序指针向量在STL中像

这个:


"

int compare(vector< string *> :: iterator a,vector< ; string *> :: iterator b){

return(** a)<(** b);

}


sort(iter,iter_end,compare);

"



你没有指定iter和iter_end的类型,但是我要打算

假设他们是类型vector< string *> :: iterator。


>

但这本身不行,我真的不喜欢知道原因。
有人能告诉我如何正确使用它吗?



问题是你的比较函数将迭代器作为

参数,它应该取值。另外,它应该返回

bool。例如:


bool compare(string * a,string * b){

return(* a)< (* b);

}


你可能考虑的另一件事是写一个通用的解引用

二元谓词适配器:


#include< functional>


模板< typename它,typename Pred>

class deref_pred:public std :: binary_function< It,It,bool>

{

public:


deref_pred(){}

deref_pred(const Pred& pred):pred(pred){}

bool operator()(const It& a,const It& b)const

{return pred(* a,* b); }


私人:


Pred pred;

};


然后你会称之为:


std :: sort(iter,iter_end,

deref_pred< std :: string *,std :: less< ; std :: string());


这在前端做了一些工作,但重用起来要容易得多。


希望这会有所帮助,

Nate


你可能会考虑的另一件事是写一个通用的解引用


二元谓词适配器:


#include< functional>


模板< typename它,typename Pred> ;

class deref_pred:public std :: binary_function< It,It,bool>

{

public:


deref_pred(){}

deref_pred(const Pred& pred):pred(pred){}

bool operator()( const它& a,const它& b)const

{return pred(* a,* b); }


私人:


Pred pred;

};


然后你会称之为:


std :: sort(iter,iter_end,

deref_pred< std :: string *,std :: less< ;的std :: string());



似乎deref_pred只是一个包装纸,真正的比较

函数需要在使用时传递给它吗?所以,什么'deref_pred在这里做了




我也对这里的继承感到困惑:public

std :: binary_function< ;它,它,bool>,

它的用途是什么?


I wanna sort a pointer vector with the algorithm "sort" in STL like
this:

"
int compare(vector<string*>::iterator a,vector<string*>::iterator b){
return (**a)<(**b);
}

sort(iter,iter_end,compare);
"
but this dosen''t work,and I really don''t know why.
Could anyone show me how to use this correctly?

解决方案

Magcialking wrote:

I wanna sort a pointer vector with the algorithm "sort" in STL like
this:

"
int compare(vector<string*>::iterator a,vector<string*>::iterator b){
return (**a)<(**b);
}


should be:

int compare(const string *a, const string *b)
{
return *a < *b;
}

sort(iter,iter_end,compare);
"
but this dosen''t work,and I really don''t know why.
Could anyone show me how to use this correctly?


Magcialking wrote:

I wanna sort a pointer vector with the algorithm "sort" in STL like
this:

"
int compare(vector<string*>::iterator a,vector<string*>::iterator b){
return (**a)<(**b);
}

sort(iter,iter_end,compare);
"

You don''t specify the type of iter and iter_end, but I am going to
assume that they''re of type vector<string*>::iterator.

>
but this dosen''t work,and I really don''t know why.
Could anyone show me how to use this correctly?

The problem is that your comparison function takes iterators as
parameters, when it should take values. Additionally, it should return
a bool. For example:

bool compare(string *a,string *b) {
return (*a) < (*b);
}

Another thing you might consider is writing a generic dereferencing
binary predicate adapter:

#include <functional>

template <typename It,typename Pred>
class deref_pred : public std::binary_function<It,It,bool>
{
public:

deref_pred() {}
deref_pred(const Pred &pred) : pred(pred) {}

bool operator()(const It &a,const It &b) const
{ return pred(*a,*b); }

private:

Pred pred;
};

Then you would call sort like:

std::sort(iter,iter_end,
deref_pred<std::string*,std::less<std::string());

This is a bit more work on the front end, but it''s much easier to reuse.

Hope this helps,
Nate


Another thing you might consider is writing a generic dereferencing

binary predicate adapter:

#include <functional>

template <typename It,typename Pred>
class deref_pred : public std::binary_function<It,It,bool>
{
public:

deref_pred() {}
deref_pred(const Pred &pred) : pred(pred) {}

bool operator()(const It &a,const It &b) const
{ return pred(*a,*b); }

private:

Pred pred;
};

Then you would call sort like:

std::sort(iter,iter_end,
deref_pred<std::string*,std::less<std::string());

it seems that deref_pred is just a wrapping paper,the real compare
function need to be pass to it when used?so, what''s deref_pred doing
here?

and I am also confuse about the inheritance here:public
std::binary_function<It,It,bool>,
what''s it for?


这篇关于关于STL排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆