C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了unordered_multimap gnu 11和c 0x中的不同行为大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下程序在不同的编译器编译,并得到不同的行为,

资源 :

#include <iostream>
#include <sstream>
#include <un@R_607_5316@_map>

using namespace std ;

std::un@R_607_5316@_map<std::string,std::string> mymap;
std::un@R_607_5316@_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 un@R_607_5316@_map1.cpp -o un@R_607_5316@_map1.exe

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

将其编译为以下内容

g++ -std=gnu++11 un@R_607_5316@_map1.cpp -o un@R_607_5316@_map1.exe

这一次,mymap仍然得到了正确的答案,但mymultimap都是空的,@H_508_11@minGw g版本是

g(rev1,由MinGW-builds项目建造)4.8.1

我对c 11感兴趣,所以我在winx下载MinGw编译器,
g 4.4.6,我的developpe编译器,无法编译c 11进行测试.

我错过了什么?我的目标是测试c 11,任何建议都表示赞赏!!

编辑:

@H_180_4@mymultimap.insert(mypair[idx]) ; //won't work !! mymultimap.insert(make_pair(s1,s2)) ; //This work !!

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

@H_262_34@

解决方法

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

这与mingw没有直接关系.使用g++4.8编译的测试用例具有相同的问题.但是,在ideone上用g 4.7.2编译的相同测试用例没有这个问题.这是g 4.8中的一个错误,你应该报告它(或者告诉我,我会为你报告).

怎么了:

@L_976_9@mymap.insert(mypair [0]);正在将字符串移出std ::对.所以,当mymultimap.insert(mypair [0]);调用,字符串已被移动.

这是一个最小测试用例:

std::un@R_607_5316@_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;
}
@H_262_34@ @H_262_34@

大佬总结

以上是大佬教程为你收集整理的unordered_multimap gnu 11和c 0x中的不同行为全部内容,希望文章能够帮你解决unordered_multimap gnu 11和c 0x中的不同行为所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。