std::map 自定义排序的问题,测试通过马上给分

szmaimu 2005-09-09 06:18:55
自定一个结构体
struct AAA
{
DWORD aaa;
int bbb;
}

map <AAA* pA ,int var> theMap

要求map 按照AAA中的aaa的大小进行排序


...全文
719 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jagen在路上 2005-09-11
  • 打赏
  • 举报
回复
to foochow,你还是学生阿,怪不得时间充足阿,呵呵!
xlsue 2005-09-11
  • 打赏
  • 举报
回复
coll["new_key"] = coll["old_key"];
coll.erase("old_key");
heguosheng 2005-09-11
  • 打赏
  • 举报
回复
这个在插入的时候排序的,如果我在后面改变aaa的值,这个排序就不起作用了,有什么方法吗
----------------------------------------
it's no way to alter the value of key, you may insert another one with your key/value after delete the original key/value.
foochow 2005-09-10
  • 打赏
  • 举报
回复
通过转化掉const当然可以改了,但是容器不在处于稳定或可用状态,代码就会出现问题,楼主真要那样做的话,可以把数据导出到vector中进行修改排序....
zlqian 2005-09-10
  • 打赏
  • 举报
回复
map元素本身是按升序排列的
zlqian 2005-09-10
  • 打赏
  • 举报
回复
1 #include <map>
2 #include <iostream>
3 #include <algorithm>
4
5 struct AAA
6 {
7 double aaa;
8 int bbb;
9 AAA(double a, int b):aaa(a),bbb(b){}
10 friend std::ostream &operator<< (std::ostream& out, const AAA& ref)
11 {
12 out<<"("<<ref.aaa<<":"<<ref.bbb<<")";
13 return out;
14 }
15
16 };
17
18 namespace std{
19 template<> struct less<AAA> : public binary_function<AAA,AAA,bool>
20 {
21 bool operator () (const AAA& _x, const AAA& _y) const { return _x.aaa<_y.aaa;}
22 };
23 }
24 using namespace std;
25
26 int main()
27 {
28 map<AAA, double, less<AAA> > theMap;
29 theMap[ AAA(0.2,20) ] = 0.2;
30 theMap[ AAA(0.5,50) ] = 0.5;
31 theMap[ AAA(0.1,10) ] = 0.1;
32 map<AAA, double, less<AAA> >::iterator ite=theMap.begin();
33 for( ; ite!=theMap.end(); ite++) cout<<(*ite).first<<"->"<<(*ite).second<<endl;
34 }
35
xlsue 2005-09-09
  • 打赏
  • 举报
回复
to jiajun2001(嘉俊):
"Key值是不能修改的,因为它被定义为const了"
可以修改的,以前讨论过了。通过黑客手段。建议不要用,我们是良民!
xlsue 2005-09-09
  • 打赏
  • 举报
回复
to foochow(恰似你的温柔):
使用指针为key,小心火烛!
foochow 2005-09-09
  • 打赏
  • 举报
回复
template <class RandomAccessIterator>
void sort(RandomAccessIterator first, RandomAccessIterator last)
map的迭代器不是mutable的,而sort要求RandomAccessIterator是mutable............

jiajun2001(嘉俊)
在实验室上课呢,无聊就上来看看,刚好碰到了,嘿嘿.....

Jagen在路上 2005-09-09
  • 打赏
  • 举报
回复
楼主应该好好看看有关stl方面的著作,Key值是不能修改的,因为它被定义为const了。
to foochow你一天就呆在CSDN上阿,你不需要工作吗???怎么每一次都会被你抢先?
szmaimu 2005-09-09
  • 打赏
  • 举报
回复
std::sort 不是可以对所有容器进行排序吗
foochow 2005-09-09
  • 打赏
  • 举报
回复
一旦一个键被插入map中,那么无论那个键被什么方式修改,它在map中的相对位置是不能改变的.如果修改了键值,map也会毫不知情,它对元素次序的假设将被推翻,查找合法元素的的操作会失败,iterator就不一定能按照键的次序来遍历map中的元素.....
szmaimu 2005-09-09
  • 打赏
  • 举报
回复
这个在插入的时候排序的,如果我在后面改变aaa的值,这个排序就不起作用了,有什么方法吗
foochow 2005-09-09
  • 打赏
  • 举报
回复
#include<iostream>
#include<map>
#include<iterator>
#include<windows.h>
using namespace std;
struct AAA
{
DWORD aaa;
int bbb;
AAA(DWORD a,int b):aaa(a),bbb(b){};
};
struct iter
{
bool operator()(const AAA*s1,const AAA*s2)
{
return s1->aaa>s2->aaa;
}
};
int main()
{
map<AAA*,int,iter>temp;
temp.insert(make_pair(&AAA(1,1),1));
temp.insert(make_pair(&AAA(2,2),2));
temp.insert(make_pair(&AAA(3,3),3));
temp.insert(make_pair(&AAA(4,4),4));
temp.insert(make_pair(&AAA(5,5),5));
map<AAA*,int,iter>::iterator it=temp.begin();
while(it!=temp.end())
{
cout<<it->first->aaa<<" "<<it->first->bbb<<" "<<it->second<<endl;
it++;
}
return 0;
}

64,282

社区成员

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

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