加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

unordered_multimap gnu 11和c 0x中的不同行为

发布时间:2020-12-16 05:04:02 所属栏目:百科 来源:网络整理
导读:我有以下程序在不同的编译器编译,并得到不同的行为, 资源 : #include iostream#include sstream#include unordered_mapusing namespace std ;std::unordered_mapstd::string,std::string mymap;std::unordered_multimapstd::string,std::string mymultimap;
我有以下程序在不同的编译器编译,并得到不同的行为,

资源 :

#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 ….虽然对我来说很奇怪~~

解决方法

BUG FILED http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57619

这与mingw没有直接关系.使用g++4.8编译的测试用例具有相同的问题.但是,在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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读