
资源 :
#include <iostream>#include <sstream>#include <unordered_map>using namespace std ;std::unordered_map<std::string,std::string> mymap;std::unordered_multimap<std::string,std::string> mymultimap;int main (){ DoAddItem() ; std::cout << "mymap contains:"; for ( auto it = mymap.begin(); it != mymap.end(); ++it ) std::cout << " " << it->first << ":" << it->second; std::cout << std::endl; std::cout << "============================================" << std::endl ; std::cout << "mymultimap contains:"; for ( auto it2 = mymultimap.begin(); it2 != mymultimap.end(); ++it2 ) std::cout << " " << it2->first << ":" << it2->second; std::cout << std::endl; return 0;} voID DoAddItem() { std::pair<std::string,std::string> mypair[100]; int IDx ; std::string s1 ; std::string s2 ; for(IDx=0;IDx<10;IDx++) { s1 = string("key") + int2str(IDx) ; s2 = string("val") + int2str(IDx) ; mypair[IDx] = {s1,s2} ; mymap.insert(mypair[IDx]) ; mymultimap.insert(mypair[IDx]) ; }//for return ; } 在RedHat linux中编译为g 4.4.6,如:
g++ --std=c++0x unordered_map1.cpp -o unordered_map1.exe
将获得mymap和mymultimap的正确答案,但在MinGw中:
http://sourceforge.net/projects/mingwbuilds/?source=dlp
将其编译为以下内容:
g++ -std=gnu++11 unordered_map1.cpp -o unordered_map1.exe
这一次,mymap仍然得到了正确的答案,但mymultimap都是空的,
MinGw g版本是
g(rev1,由MinGW-builds项目建造)4.8.1
我对c 11感兴趣,所以我在winx下载MinGw编译器,
g 4.4.6,我的developpe编译器,无法编译c 11进行测试.
我错过了什么?我的目标是测试c 11,任何建议都表示赞赏!!
编辑:
mymultimap.insert(mypair[IDx]) ; //won't work !!mymultimap.insert(make_pair(s1,s2)) ; //This work !!
将代码更改为make_pair后,它在MinGw中工作并打印正确的答案
对于mymultimap ….虽然对我来说很奇怪~~
这与mingw没有直接关系.使用@L_404_2@编译的测试用例具有相同的问题.但是,在IDeone上用g 4.7.2编译的相同测试用例没有这个问题.这是g 4.8中的一个错误,你应该报告它(或者告诉我,我会为你报告).
怎么了:
调用mymap.insert(mypair [0]);正在将字符串移出std ::对.所以,当mymultimap.insert(mypair [0]);被调用,字符串已被移动.
这是一个最小测试用例:
std::unordered_map<std::string,std::string> mymultimap;int main (){ std::pair<std::string,std::string> mypair[1]; std::string s1 = std::string("key"); std::string s2 = std::string("val"); mypair[0] = {s1,s2}; mymap.insert(mypair[0]); mymultimap.insert(mypair[0]); std::cout << "mymultimap contains:"; for ( auto it2 = mymultimap.begin(); it2 != mymultimap.end(); ++it2 ) std::cout << " " << it2->first << ":" << it2->second;} 总结 以上是内存溢出为你收集整理的unordered_multimap gnu 11和c 0x中的不同行为全部内容,希望文章能够帮你解决unordered_multimap gnu 11和c 0x中的不同行为所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)