求解答:关于multimap赋值问题

chengyongyuan1111 2010-07-01 01:35:44
程序如下:想用multimap来存放自己定义的一个类,可是无法用下标进行赋值。完整代
码如下(vs2008),有没有遇到过。 A的拷贝构造,赋值运算符,还有比较运算符定不定义,错误都是一样的。

#include <map>
#include <string>
#include <iostream>
using namespace std;


class A
{
public:
A(const string& _name, int _no) :
name(_name), no(_no)
{
}
A() {}
A(const A& aa)
{
name = aa.name;
no = aa.no;
}

A& operator=(const A& aa)
{
name = aa.name;
no = aa.no;
return *this;
}
friend bool operator<(A& a, A& aa);
friend bool operator==(A& a, A& aa);


private:
string name;
int no;
};

bool operator<( A& a, A& aa)
{
return a.name < aa.name;
}

bool operator==( A&a, A& aa)
{
if ( a.name == aa.name )
return true;
else
return false;
}

void main()
{
A s("silver",10001);
A s2("silver",10002);
std::multimap<string,A> m;
//cout << (s == s2 )<< endl;
m.insert(make_pair("silver",s));
//m.insert(make_pair("silver",s2));
m["silver"] = s2;
}

错误信息:d:\我的文档\visual studio 2008\projects\testmultimap\main.cpp(70) :
error C2676: 二进制“[”: “std::multimap<_Kty,_Ty>”不定义该运算符或到预定
义运算符可接收的类型的转换
1> with
1> [
1> _Kty=std::string,
1> _Ty=A
1> ]
1>生成日志保存在“file://d:\我的文档\Visual Studio 2008\Projects\
TestMultimap\Debug\BuildLog.htm”
1>TestMultimap - 1 个错误,0 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
...全文
275 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
P_ghost 2010-07-04
  • 打赏
  • 举报
回复
使用insert
孤之雨 2010-07-04
  • 打赏
  • 举报
回复
你把友元函数的定义放在直接放在类的内部,vc++6.0处理友元时,如果没装sp6补丁会有点问题~~~
pengzhixi 2010-07-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cattycat 的回复:]
multimap没有重载[]运算符。
[/Quote]
up,对于键值可以重复的重载[]没有意义
yunyun1886358 2010-07-03
  • 打赏
  • 举报
回复
根本原因是multimap不支持下标操作符。map的[]参数需要是键值,map的键值是唯一的,所以可以用[key]找到元素。但是multimap的键值不唯一,m["silver"] 找到的元素可能也不唯一,如果m["silver"] 找到多个元素,那么m["silver"] = s2;到底为哪一个元素赋值呢?编译器不知道,所以编译不通过。
cattycat 2010-07-03
  • 打赏
  • 举报
回复
multimap没有重载[]运算符。
djjlove_2008 2010-07-03
  • 打赏
  • 举报
回复
#include <map> 
#include <string>
#include <iostream>
using namespace std;


class A
{
public:
A(const string& _name, int _no) :
name(_name), no(_no)
{
}
A() {}
A(const A& aa)
{
name = aa.name;
no = aa.no;
}

A& operator=(const A& aa)
{
if(this == &aa)
delete[] this; //有必要加这句
name = aa.name;
no = aa.no;
return *this;
}
friend bool operator<(A& a, A& aa);
friend bool operator==(A& a, A& aa);


private:
string name;
int no;
};

bool operator<( A& a, A& aa)
{
return a.name < aa.name;
}

bool operator==( A&a, A& aa)
{
if ( a.name == aa.name )
return true;
else
return false;
}
int main()
{
A s("silver",10001);
A s2("silver",10002);
std::map<string,A> m; //这个地方要改
//cout << (s == s2 )<< endl;
m.insert(make_pair("silver",s));
//m.insert(make_pair("silver",s2));
m["silver"] = s2;

system("pause");
return 0;
}

为什么不能用multimap,只能用map容器相信你从他们的概念上就能明白(因为map是键值对,后面的表示插入是否成功,而multimap允许一个键对应多个值吧,从逻辑上程序中那个赋值语句应该不成立)呵呵,但我这样改应该改变了你程序的初衷,希望对你有一点帮助。
tan625747 2010-07-03
  • 打赏
  • 举报
回复
这个真要,帮顶一下

64,681

社区成员

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

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