map容器对象插入数据的4种方式讲解

来源:互联网 发布:网络复活赛 编辑:程序博客网 时间:2024/05/20 22:03

map容器对象插入数据的4种方式讲解


map容器对象插入数据的4种方式

#include <string>

#include <iostream> 

#include <map> 

#include <utility> 

using namespace std;

int main()

{

    map<intstringEmployee;

    //通过键值赋值

    Employee[123] = "Mayuefei";

    //通过成员函数insert和STL的pair赋值

    Employee.insert(pair<intstring>(132, "Liaoyuanqing"));

 

    //通过value_type赋值

    Employee.insert(map<intstring>::value_type(124, "Liyiyi"));

 

    //通过make_pair赋值

    Employee.insert(make_pair(234, "LLK.D"));

    for (map<intstring>::iterator it = Employee.begin(); it != Employee.end(); it++)

    {

       cout<<(*it).first<<":"<<(*it).second<<endl;//取值操作

 

    }

    system("pause");

    return 1;

};

0 0
原创粉丝点击