C++mapの3つの異なる挿入要素の方法

1626 ワード

Talk is cheap. Show you the code!
#include 
#include 
using namespace std;

int main(){
    map student;
    student.insert(map::value_type(1,"liMing"));//   
    student.insert(pair(2,"wanglei"));//   
    student[3] = "pang";//   

    map::iterator iter;
    for(iter = student.begin();iter != student.end(); ++iter){
        cout << iter->first<second<::iterator,bool> insert_pair;
    insert_pair = student.insert(pair(1,"zpz"));
    if(insert_pair.second == true)
        cout << "Insert successfully!"<

実行結果:
1 liMing
2 wanglei
3 pang
Insert failure!

上記の3つの違いは何ですか?
最初の2つはいずれもinsertを用いており,何の違いもないが,3つ目の違いはデータの挿入に集合の一意性という概念にかかわる.すなわちmapにこのキーワードがある場合、insert操作は挿入できません.しかし、配列で元のデータを直接上書きします.
#include 
#include 
using namespace std;

int main(){
    map student;
    student.insert(map::value_type(1,"liMing"));
    student.insert(pair(2,"wanglei"));
    student[3] = "pang";

    map::iterator iter;
    for(iter = student.begin();iter != student.end(); ++iter){
        cout << iter->first<second<::iterator iter2;
    for(iter2 = student.begin();iter2 != student.end(); ++iter2){
        cout << iter2->first<second<