map_插入数据_four_means

//

pair< map< T1,T2 >::iterator,bool > it; //保存insert()的返回值  

    eg.
        pair< map<int,string>::iterator,bool > it;  

01 mp.insert( pair< T1,T2 >( value1,value2 ) );

    eg.
        it=mp.insert( pair<int,string>( 1,"abcd" ) );
        if( it.second ) cout<<"yes!"<<endl;
        else            cout<<"no!"<<endl;

02 mp.insert( make_pair( T1 value1,T2 value2 ) );

    eg.
        it=mp.insert( make_pair( 2,"efg" ) );
        if( it.second ) cout<<"yes!"<<endl;
        else            cout<<"no!"<<endl;

        it=mp.insert( make_pair( 2,"_efg_" ) );     //插入失败,不会产生覆盖 
        if( it.second ) cout<<"yes!"<<endl;
        else            cout<<"no!"<<endl;

03 mp,insert( map< T1,T2 >::value_type( value1,value2 ) );

    eg.
        it=mp.insert( map<int,string>::value_type( 3,"hijk" ) );

04 mp[ pos ]=value;

    eg.
        mp[4]="lmn";
        mp[4]="_lmn_";  //覆盖

attention:

    01 前3种方法 mp.insert() return pair<iterator,bool> 
                        重复插入时 bool == false 表示插入失败 且不会覆盖原有值

    02 第4种方法 mp[] 重复插入时 将覆盖原有值 

猜你喜欢

转载自blog.csdn.net/qq_63173957/article/details/123615906