怎么用insert函数给map容器添加元素?
m1.insert(make_pair("lucy",20));改成m1.insert(make_pair(string("lucy"),20));
make_pair是std::pair的helper function,是个函数模板,根据参数确定匹配的pair的元素类型,所以LZ的用法弄出来的元素是pair<char* int>类型的。
insert函数
string& insert ( size_t pos1, const string& str, size_t pos2, size_t n );
Inserts a copy of a substring of str at character position pos1. The substring is the portion of str that begins at the character position pos2 and takes up to n characters (it takes less than n if the end of str is reached before).
string& insert ( size_t pos1, const string& str, size_t pos2, size_t n );
Inserts a copy of a substring of str at character position pos1. The substring is the portion of str that begins at the character position pos2 and takes up to n characters (it takes less than n if the end of str is reached before).