如何关于使用 std::sort 对容器存储的指针进行排序

hzhxxx 2011-04-29 10:38:08
如何关于使用 std::sort 对容器存储的指针进行排序

struct _row
{
std::string m_name;
int m_age;
std::string m_sex;
bool operator< (const _row &r) const
{
return m_name.compare(r.m_name) >= 0?true:false;
}

bool operator< (const _row *r) const
{
return m_name.compare(r->m_name) >= 0?true:false;
}
};

void test_sort_by_point()
{
std::vector<_row *> r;
_row *r1 = new _row();
r1->m_age = 2;
r1->m_name = "aaaa";
r1->m_sex = "男";
r.push_back(r1);

r1 = new _row();
r1->m_age = 4;
r1->m_name = "dddd";
r1->m_sex = "男";
r.push_back(r1);

r1 = new _row();
r1->m_age = 1;
r1->m_name = "bbbb";
r1->m_sex = "男";
r.push_back(r1);

_row *r2 = new _row();
r2->m_age = 9;
r2->m_name = "cccc";
r2->m_sex = "男";
r.push_back(r2);

std::vector<_row *>::iterator it;
for(it=r.begin();it != r.end();++it)
{
std::cout<<(*it)->m_name<<" ";
}
std::cout<<endl;

//r.sort(std::less<_row*>());
std::sort(r.begin(),r.end());
for(it=r.begin();it != r.end();++it)
{
//_row * t = *it;
std::cout<<(*it)->m_name<<" ";
}
std::cout<<endl;
}

int main(int argc,char *argv[])
{
test_sort_by_point();
system("pause");
}

现在输出的结果是:
aaaa dddd bbbb cccc
aaaa dddd bbbb cccc
我希望得到的结果是:
aaaa dddd bbbb cccc
dddd cccc bbbb aaaaa
或者:
aaaa dddd bbbb cccc
aaaa bbbb cccc dddd

如果 std::vector<_row *> r,这个声明成 std::vector<_row> r;
就能很好的排序,达到我要的目的,按 _row 里面 m_name 排序,但是现在
r 里面存储的是指针,不能重载 bool operator<(const _row *,const _row *),
所以默认sort 里面的比较是两个指针的比较,不是指针所指的对象的比较,有什么
办法可以按对象比较呢
...全文
775 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunandsong 2013-04-23
  • 打赏
  • 举报
回复
嗯嗯 好用
h100037 2011-04-29
  • 打赏
  • 举报
回复
sort , 仿函数
hzhxxx 2011-04-29
  • 打赏
  • 举报
回复


谢谢,我自己已经实现了,一下子卡住了,没有想明白。

pengzhixi

(向taodm学习)

的方法比较简单
pengzhixi 2011-04-29
  • 打赏
  • 举报
回复
bool myfunction (_row* i,_row* j) { return !(*i<*j); }
hzhxxx 2011-04-29
  • 打赏
  • 举报
回复

template <class _Ty> struct compare_row : public std::binary_function<_Ty, _Ty, bool>
{
int m_index;
compare_row(int index):m_index(index)
{
//
}
bool operator()(const _row &_Left,const _row &_Right) const
{
bool result = false;
if(m_index == 0)
{
result = _Left->m_name.compare(_Right->m_name) >= 0?true:false;
}
else if(m_index == 1)
{
result = _Left->m_age <= _Right->m_age?true:false;
}
else
{
result = false;
}
return result;
}

bool operator()(const _Ty *r1,const _Ty *r2) const
{
bool result = false;
if(m_index == 0)
{
result = r1->m_name.compare(r2->m_name) >= 0?true:false;
}
else if(m_index == 1)
{
//result = _Left->m_age <= _Right->m_age?true:false;
}
else
{
result = false;
}
return result;
}
};


写这个函数,void test_sort_by_point() 里面的 //std::sort(r.begin(),r.end());
改写成 std::sort(r.begin(),r.end(),compare_row<_row>(0));
寒冰雪 2011-04-29
  • 打赏
  • 举报
回复
自己写个比较函数,作为sort的第三个参数。。
pengzhixi 2011-04-29
  • 打赏
  • 举报
回复
bool myfunction (_row* i,_row* j) { return (*i<*j); }

std::sort(r.begin(),r.end(),myfunction);
hzhxxx 2011-04-29
  • 打赏
  • 举报
回复


sort(ctn.begin(), ctn.end(),less<_row>()) ;

sort(ctn.begin(), ctn.end(), greater<_row>()) ;

这个肯定是不行的,他们还是直接比较了指针。
hhh_hao 2011-04-29
  • 打赏
  • 举报
回复
sort(ctn.begin(), ctn.end(),less<_row>()) ;

sort(ctn.begin(), ctn.end(), greater<_row>()) ;
colorfulcode 2011-04-29
  • 打赏
  • 举报
回复
对比指针指向的东西就好了

我记得std::sort好像可以设定对比条件,具体的忘了

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧