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

bar*_*hen 5 c++ gcc mingw c++11

我有以下程序在不同的编译器编译,并得到不同的行为,

资源 :

#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 ; 
}
Run Code Online (Sandbox Code Playgroud)

在RedHat Linux中用g ++ 4.4.6编译,如:

g++  --std=c++0x unordered_map1.cpp -o unordered_map1.exe 
Run Code Online (Sandbox Code Playgroud)

将获得mymap和mymultimap的正确答案,但在MinGw中:http://sourceforge.net/projects/mingwbuilds/? source = dlp

将其编译为以下内容:

g++ -std=gnu++11 unordered_map1.cpp -o unordered_map1.exe
Run Code Online (Sandbox Code Playgroud)

这一次,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 !!
Run Code Online (Sandbox Code Playgroud)

在我将代码更改为make_pair之后,它在MinGw中工作并为mymultimap打印正确的答案....虽然这对我来说很奇怪~~

Jes*_*ood 3

已提交错误 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::pair. 所以,当mymultimap.insert(mypair[0]);被调用时,琴弦已经被移动了。

这是一个最小的测试用例:

std::unordered_map<std::string,std::string> mymap;
std::unordered_multimap<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;
}
Run Code Online (Sandbox Code Playgroud)