C++之自定义key类型,重载操作符
#include <map>
#include <string>
using namespace std;
class MyString
{
public:
MyString(){m_string = "";}
MyString(string& str){m_string = str;}
~MyString(){}
string GetString() const { return m_string;} /*const 是刚刚加上的*/
void SetString(string& str) {m_string = str;}
bool operator < (const MyString& myStr) const /*参数中的const 是刚刚加上的*/
{
return m_string.compare(myStr.GetString()) < 0;
}
private:
string m_string;
};
int _tmain(int argc, _TCHAR* argv[])
{
map<MyString,string> MyMap;
string name = "Name";
MyString str1(name);
MyMap[str1] = "Kevin";
return 0;
}
只想当咸鱼的程序员