[问题] C++ map的用法(solved)

楼主: chigi (  )   2014-07-14 17:17:37
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
VC++, G++
问题(Question):
我期望可以像python的Dic有一样(或是类似的功能)
也就是 map[key] = value 这样的用法,
在C++论坛上是说用map实作是没问题的。
不过我实际在implement的时候,
当我的key 型态(或是value)是std::string的时候,
compile就会出现错误,错误码是
error C2784: 'bool std::operator <(const std::move_iterator<_RanIt> &,const
std::move_iterator<_RanIt2> &):
无法由 'const std::string',针对 'const std::move_iterator<_RanIt> &' 推算
样板 引数
(类似的有四项,包括reverse_iterator, _Revanit, pair)

error C2676: 二元运算子 '<' : 'const std::string' 没有定义此运算子或预先定义运
算子可接受的型别转换
但是如果把 std::string 的部分换成 char * 编译就没问题
网络上找到std::string的example也都是编不过....
但是这个设计,不能使用位址map(使用char*),毕竟每个新的char * 位址都不同
请问各位,要用 std::string map 的话,应该调整什么?
程式码(Code):(请善用置底文网页, 记得排版)
<partial>
std::string string1, string2;
string1 = "parameter";
string2 = "value";
std::map<std::string, std::string> mymap;
mymap.insert(std::pair<std::string, std::string>(string1, string2));
</partial>
作者: LPH66 (-6.2598534e+18f)   2014-07-14 17:36:00
理论上这应该要没问题...你的 VC++ 版本是?另外 STL 提供了 make_pair 让你省去写 std::pair<> 的麻烦
楼主: chigi (  )   2014-07-14 17:39:00
visual 2012, 如果可以希望把这个做成到处用的cpp档..
作者: LPH66 (-6.2598534e+18f)   2014-07-14 17:41:00
虽然可能不太相关, 你有 #include <string> 吗?因为这看起来像是编译器找不到 operator<(string,string)
楼主: chigi (  )   2014-07-14 17:46:00
恩,我没有include <string> XD 我试试看。过..过了,来人阿给我一把刀,我要切腹。谢谢LPH66
作者: Killercat (杀人猫™)   2014-07-15 05:05:00
其实就算再绝望也不该传入char*, 这是危险动作一般来讲需要要当key的话 会hash这个char*内容字串要当value的话,请务必用string

Links booklink

Contact Us: admin [ a t ] ucptt.com