在C++中设置映射*的键和值
Set key and value of a map* in C++
我在下面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;
}
但是,对于像这样的非指针类型的地图来说,通常情况下,问题是
std::map<char,int> first;
如中所述http://www.cplusplus.com/reference/map/map/map/,我看到我们可以做
first['a']= 10;
但是如果我们有一个map的指针类型,我们怎么能做到呢我上面的代码将生成一个错误报告
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
您可以取消引用指针:
(*m)[10] = 1;
或
m->operator[](10) = 1;
另一方面,你真的需要所有这些指针吗?您的程序会因为使用此表单而受到很大影响吗?
map<unsigned long long int, map<int, int>> invertID;
int main()
{
map<int,int> m;
invertID[1][10] = 1;
}
Map是一个红黑树,其中包含操作,因此您可以使用它来获得最佳搜索和插入性能。。等等。您在inversidID中使用的是贴图而不是多贴图。因此,在大多数情况下,当您获取某个密钥时,我会假设您正在寻找一对元素。除非你出于我现在没有想到的任何原因需要第二张地图,否则我会选择这个:
std::map < unsigned long long int, std::pair < int , int > > invertID;
int main(){
std::pair<int,int> m;
m = std::make_pair(1,2);
invertID[1] = m;
return 0;
}
玩得开心。
相关文章:
- 通过从矢量中获取键和与映射中的键对应的值来创建映射
- 在具有 std::p air 键和值的映射中添加 b2vec2
- 如何将键和值从映射传输到另一个映射?
- 如何通过键和值以及在C 中的结果映射中比较两个地图?我们有任何STL API吗?
- 多重映射的每个元素是否同时包含键和值
- 如何正确使用带有智能指针和自定义类的映射作为键和值
- 如何使用 boost 连接映射的键和值,特别是如果值是结构
- 对象来映射键和值
- C++STL映射键和值工作不正常
- 使用C++中的函数指针映射枚举键和值
- 从变量映射键和值初始化
- 键和值属于同一类型的多重映射是否有任何意义
- 如何在C++中创建将键和值作为结构的映射
- 使用结构作为映射的键和值.find() 操作给出错误
- 从常量迭代器到映射向量,获取映射向量中映射元素的键和值
- std::映射绑定到所有键和值的堆的分配器
- 如何在c++中比较映射中的键和值
- 在c++中如何从另一个完整映射中插入键和值到空映射
- 在c++中交换映射的键和值
- 交换映射中的键和值