64,681
社区成员
发帖
与我相关
我的任务
分享
#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;
}
#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;
}