我正在关注http://www.cplusplus.com/reference/map/map/map/。
我正在尝试建立一个反向索引结构,该结构是一个具有64位整数作为键的映射,并且每个键将包含一个指向子映射的指针。子映射将包含int int对。所以我写了一些样本:
map<unsigned long long int, map<int, int>*> invertID;
int main(int argc, char *argv[]){
map<int,int>* m = new map<int,int>();
m[10] = 1;
invertID[1] = m;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但这通常是非指针类型映射的麻烦
std::map<char,int> first;
Run Code Online (Sandbox Code Playgroud)
如http://www.cplusplus.com/reference/map/map/map/中所述,我看到我们可以做到
first['a']= 10;
Run Code Online (Sandbox Code Playgroud)
但是,如果我们有一个map的指针类型,那我们该怎么做呢?我上面的代码将产生错误提示
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
Run Code Online (Sandbox Code Playgroud)
您可以取消引用指针:
(*m)[10] = 1;
Run Code Online (Sandbox Code Playgroud)
要么
m->operator[](10) = 1;
Run Code Online (Sandbox Code Playgroud)
另一方面,您真的需要所有这些指针吗?使用此表格会严重影响您的程序吗?
map<unsigned long long int, map<int, int>> invertID;
int main()
{
map<int,int> m;
invertID[1][10] = 1;
}
Run Code Online (Sandbox Code Playgroud)