c++中容器map怎么插入一对元素?
来源:百度知道 编辑:UC知道 时间:2024/05/29 06:34:15
比如我申明了一个:map<int,string> output,怎么向map里面插入
首先包含头文件
#include <string>
#include <map>
using namespace std;
如下写法均合法:
map<string, int> word_count;
word_count["string_1"] = 1;
word_count.insert(make_pair<string, int>("string_2", 2));
word_count.insert(map<string, int>::value_type("string_3", 3));
在Visual Studio 2010中编译通过。
map<int,string> output;
output.insert(pair<int,string>(1,"heheheh"));
//vc6 pass
#include <iostream>
#include <map>
#include <string>
using namespace std;
void main()
{
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, "student_one"));
mapStudent.insert(pair<int, string>(2, "student_two"));
mapStudent.insert(pair<int, string>(3, "student_three"));