STL Map按value排序,注意此Map的key是结构体

chengdu1983 2011-08-12 10:04:17
map已经建立好了,重载了‘<’,最好用sort排序,如果说什么冒泡排序就没什么意义了。
...全文
1229 46 打赏 收藏 转发到动态 举报
写回复
用AI写文章
46 条回复
切换为时间正序
请发表友善的回复…
发表回复
chengdu1983 2011-09-01
  • 打赏
  • 举报
回复
不好意思,你这个方法好像不行,我的方法可以,只是类的问题没弄清楚。[Quote=引用 45 楼 terhack 的回复:]
麻烦,你把vlaue当Key就行,然后value改成array,相同key的塞到array里,map自动排序
[/Quote]
terhack 2011-08-15
  • 打赏
  • 举报
回复
麻烦,你把vlaue当Key就行,然后value改成array,相同key的塞到array里,map自动排序
kingxujs 2011-08-15
  • 打赏
  • 举报
回复
来学习的
chengdu1983 2011-08-12
  • 打赏
  • 举报
回复
结贴了,谢谢大家。
chengdu1983 2011-08-12
  • 打赏
  • 举报
回复
牛x,确实是这样,编译没问题了,我正在试结果。[Quote=引用 41 楼 luciferisnotsatan 的回复:]
引用 39 楼 chengdu1983 的回复:

C/C++ code
inline int cmp( std::pair<Palette, int>&amp; x, std::pair<Palette, int>&amp; y)
{
return x.second > y.second;
}
引用 37 楼 luciferisnotsatan 的回复:
Rend……
[/Quote]
luciferisnotsatan 2011-08-12
  • 打赏
  • 举报
回复
[Quote=引用 39 楼 chengdu1983 的回复:]

C/C++ code
inline int cmp( std::pair<Palette, int>& x, std::pair<Palette, int>& y)
{
return x.second > y.second;
}
引用 37 楼 luciferisnotsatan 的回复:
Render:……
[/Quote]

inline int cmp( std::pair<Palette, int>& x, std::pair<Palette, int>& y) { return x.second > y.second; }
这个是类里的,还是单独的全局函数??
如果是类里的,而且没有static,那么参数里隐含一个this指针

实际上就是inline int cmp( 类名 *this, std::pair<Palette, int>& x, std::pair<Palette, int>& y) { return x.second > y.second; } 这么个格式。这样就有问题了。
pengzhixi 2011-08-12
  • 打赏
  • 举报
回复
cmp直接写成一个全局的函数。传递的时候直接传递cmp函数名即可。
chengdu1983 2011-08-12
  • 打赏
  • 举报
回复
inline int cmp( std::pair<Palette, int>& x,  std::pair<Palette, int>& y)  
{
return x.second > y.second;
}
[Quote=引用 37 楼 luciferisnotsatan 的回复:]
Render::RenderFactoryBase::cmp
前面的是namespace还是类?如果是类的话,cmp是不是static的?
把这个的代码也贴下
[/Quote]
chengdu1983 2011-08-12
  • 打赏
  • 举报
回复
是呀,默认的‘=’也是一样用,回头还要写个‘==’,有默认的我就没弄。[Quote=引用 36 楼 luciferisnotsatan 的回复:]
Render::RenderFactoryBase::cmp这个是什么?你写的那个cmp函数??
应该没问题呀,Palette没含指针,默认的复制构造函数够用了。

出什么问题了呢??
[/Quote]
luciferisnotsatan 2011-08-12
  • 打赏
  • 举报
回复
Render::RenderFactoryBase::cmp
前面的是namespace还是类?如果是类的话,cmp是不是static的?
把这个的代码也贴下
luciferisnotsatan 2011-08-12
  • 打赏
  • 举报
回复
Render::RenderFactoryBase::cmp这个是什么?你写的那个cmp函数??
应该没问题呀,Palette没含指针,默认的复制构造函数够用了。

出什么问题了呢??
chengdu1983 2011-08-12
  • 打赏
  • 举报
回复

复制构造函数和operator=没有自己写。[Quote=引用 33 楼 luciferisnotsatan 的回复:]
int cmp(const std::pair<Palette, int>& x, const std::pair<Palette, int>& y)
{
return x.second > y.second;
}

你这个不对么??
Palette这个类里有自己写了复制构造函数和operator=吧。
[/Quote]
chengdu1983 2011-08-12
  • 打赏
  • 举报
回复
typedef struct tagPalette {
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
bool operator <(tagPalette const& _A) const
{//这个函数指定排序策略
if(rgbBlue<_A.rgbBlue && rgbGreen <=_A.rgbGreen && rgbRed <= _A.rgbRed) return true;
if( rgbGreen < _A.rgbGreen && rgbBlue<=_A.rgbBlue && rgbRed <= _A.rgbRed) return true;
if (rgbRed < _A.rgbRed && rgbBlue<=_A.rgbBlue && rgbGreen <=_A.rgbGreen ) return true;
return false;
}

} Palette;
typedef std::map<Palette,int> PaletteCount
std::vector<std::pair<Palette,int> > tVector;
for (std::map<Palette, int>::iterator curr = m_palcount.begin(); curr != m_palcount.end(); curr++)
{
tVector.push_back(std::make_pair(curr->first, curr->second));
}

std::sort(tVector.begin(), tVector.end(), &Render::RenderFactoryBase::cmp);
[Quote=引用 30 楼 pengzhixi 的回复:]
直接发代码吧,我也不想猜测你的编译错误时什么。
[/Quote]
luciferisnotsatan 2011-08-12
  • 打赏
  • 举报
回复
int cmp(const std::pair<Palette, int>& x, const std::pair<Palette, int>& y)
{
return x.second > y.second;
}

你这个不对么??
Palette这个类里有自己写了复制构造函数和operator=吧。
tangyao5d 2011-08-12
  • 打赏
  • 举报
回复
学习一下!
chengdu1983 2011-08-12
  • 打赏
  • 举报
回复
这个用试吗?是按key的。[Quote=引用 22 楼 luciferisnotsatan 的回复:]
main函数,sort之后,再遍历下map,看看按什么排的

C/C++ code

for (std::map<std::string, int>::iterator curr = tMap.begin(); curr != tMap.end(); curr++)
{
cout<<curr->first<<" "<<curr->second;
}




引用 2……
[/Quote]
pengzhixi 2011-08-12
  • 打赏
  • 举报
回复
直接发代码吧,我也不想猜测你的编译错误时什么。
chengdu1983 2011-08-12
  • 打赏
  • 举报
回复
这么专业呀,其实就是一个结构体中包含3个unsigned char和一个int[Quote=引用 27 楼 rendao0563 的回复:]
数据结构发出来. 帮你重新设计下.
[/Quote]
chengdu1983 2011-08-12
  • 打赏
  • 举报
回复
好吧 ,直接说的简单点,就是vector中的pair中有结构体<struct, int>,如何按照第二个int排序。[Quote=引用 25 楼 luciferisnotsatan 的回复:]
引用 21 楼 chengdu1983 的回复:

恩,我是完全按照他那个弄的,就是把他的string换成了结构体。引用 20 楼 luciferisnotsatan 的回复:
仔细看下他的代码。

C/C++ code

//示例代码:输入单词,统计单词出现次数并按照单词出现次数从多到少排序
#include <cstdlib>
#include <map>
#include……
[/Quote]
rendao0563 2011-08-12
  • 打赏
  • 举报
回复
数据结构发出来. 帮你重新设计下.
加载更多回复(26)

64,637

社区成员

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

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