我想测试增量++
是否适用于std::map
:
#include <bits/stdc++.h>
using namespace std;
int main()
{
map<string, int> map;;
map['1'] = 0;
map['1']++;
cout << map['1'] << endl;
return 0;
}
并得到标题的错误:
map['1'] = 0;
我不明白为什么
在C++中,'1'
是一个字符,而不是字符串文字,并且映射有一个键std::string
,这是不同的类型。这就是std::map::operator[]
给你编译器错误的原因,它是不匹配类型。
您需要""
来提及它是一个字符串文字。
map["1"] = 0;
另外,读取:
- 为什么是"使用命名空间std"被认为是不好的做法
- 为什么我不应该#include<bits/stdc++.h>