typedef map <int, string> MAP_INT_STRING;
MAP_INT_STRING mapIntToString;
mapIntToString.insert (MAP_INT_STRING::value_type (3, “Three”));
我只找到了通过源代码将值插入 map 的示例。我想知道如何在程序运行时允许用户这样做。我想象这会涉及某种 for 循环,但我不确定如何设置它。
最佳答案
int main() {
using namespace std;
map<int, string> m;
cout << "Enter a number and a word: ";
int n;
string s;
if (!(cin >> n >> s)) {
cout << "Input error.\n";
}
else {
m[n] = s;
// Or: m.insert(make_pair(n, s));
}
return 0;
}
关于c++ - 将值插入 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4987845/