如何创建仍然可以由[]运算符访问的常量值的std :: map? [英] How to create a std::map of constant values which is still accessible by the [] operator?

查看:202
本文介绍了如何创建仍然可以由[]运算符访问的常量值的std :: map?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个只读的std:地图数据结构,这意味着我必须用数据填充一次,然后只读取这些值,永远不要更改它们或添加其他值。



我的非const版本如下所示:

  // in .h 
#include< string>
#include< map>

std :: map< std :: string,int> MYMAP;
void initMap();

// in .cpp
#includefoo.h

void initMap(){
myMap [Keys] = 42;
}

然后我会调用 initMap() code>一次在我的代码,并完成。



现在我已经阅读了几个问题,似乎不寻常的实现常量的



使其成为 std :: map< std :: string,const int> 不允许我填写在$ code> initMap()中。
使用非const temp填充它,并且定义上的复制构造函数也不起作用,因为复制构造函数不容易将非const版本作为输入。



使它成为一个 const std :: map< std :: string,int> (我可以在定义期间填充一个非常量副本)禁止使用 [] 运算符进行值访问。



所以有一种方法来实现(值)常量和初始化结构(最好在头文件中)?



BTW:C ++ 0x和C ++ 11都不是$ code> 是一个选项。

解决方案

无法使用 insert()方法可用于 std :: map



http://www.cplusplus.com/reference/map/map/insert/



编辑:(解决方案) myMap.insert(std :: pair< std :: string,const int>(Keys ,42));



据我所知,这样做的原因是因为该对的构造函数 pair(const first_type& a,const second_type& b),使用<$ c $的构造函数初始化其成员第一个第二个 c> first_type 和 second_type ,采取 a b 作为它们各自的参数。



使用您尝试使用的解决方案,我的理解是 myMap [Keys ] = 42; 初始化成员第二个 c c $ c> const int )使用 int 的默认构造函数。然后尝试将该值分配给该成员。由于这是在类映射的构造函数之外完成的,所以 const 声明使其不可能。



使用 insert()的解决方案,成员将在。因此,它们可以被声明为 const 。在复制到地图之前,会执行相同的操作。


I need a std:map data structure that is read only, which means I have to fill it once with data and then only read those values, never change them or add additional ones.

My non-const version looks like this:

//in .h
#include <string>
#include <map>

std::map<std::string, int> myMap;
void initMap();

//in .cpp
#include "foo.h"

void initMap() {
  myMap["Keys"] = 42;
}

Then I'd call initMap() once in my code and be done.

Now I've read several questions here already and it seems non-trivial to achieve const-ness for the map.

Making it a std::map<std::string, const int> won't allow me to fill it in the initMap(). Filling it with a non-const temp and the copy constructor on definition doesn't work either, as the copy constructor doesn't easily take the non-const version as input.

Making it a const std::map<std::string, int> (which I could fill with a non-const copy during definition) would disable the use of the [] operator for value access.

So is there a way to achieve (value) const-ness and initialize the structure (preferably in the header file)?

BTW: Neither C++0x nor C++11 nor boost:: is an option.

解决方案

Couldn't you use the insert() method available for std::map?

http://www.cplusplus.com/reference/map/map/insert/

Edit : (solution) myMap.insert(std::pair<std::string, const int>("Keys", 42));

As far as I understand it, the reason why this works is because the constructor for the pair, pair (const first_type& a, const second_type& b), initializes its members first and second using the constructors for first_type and second_type, taking a and b as their respective parameter.

With the solution you were trying to use, my comprehension is that myMap["Keys"] = 42; initializes the member second of the map (of type const int) using the default constructor for int. Then a value is attempted to be assigned to that member. As this is done outside the constructor of the class map, the const declaration makes this impossible.

With the solution using insert(), the members are initialized in the constructor of the pair. Thus they can be declared const. The same operation is done when the pair is copied to the map.

这篇关于如何创建仍然可以由[]运算符访问的常量值的std :: map?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆