STL映射存储搜索的键
我刚刚发现,当我搜索这样的地图时:
std::map<std::string, int> aMap;
我搜索的键开始成为地图的一部分。 在上述情况下,值存储为零。 如果是指针,它将值存储为 0 值指针,
我正在使用 [] 运算符进行搜索,例如:
int a = aMap["some key"];
你能确认这一点吗? 我想我误解了 [] 运算符。 这是在做作业吗?!
在哪里可以找到此类“功能”的 STL 文档?
I have just found out that when I search a map like :
std::map<std::string, int> aMap;
the keys I search start to be part of the map. In the case above the values are stored as zeros. In case of pointers it stores the values as 0 valued pointers
I am doing the search with the [] operator, like in :
int a = aMap["some key"];
Can you confirm this ? I think I have misinterpreted the [] operator. Is it doing an assignment ?!
Where can I find STL documentation of these kind of "features" ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您是否使用 [] 运算符进行搜索? 如果是这样,那么是的,这就是定义的行为。
如果您不希望出现此行为,则应该使用“查找”方法。
Nicolai Josuttis 的书是 STL 的一个很好的参考资料。
Are you searching it with the [] operator? If so, then yes, this is the defined behaviour.
You should use the 'find' method if you don't want this behaviour.
A good reference for the STL is the Nicolai Josuttis book.
如果您使用 [] 运算符查看地图,那么,是的,您将生成默认对象。 另一方面,如果您使用“查找”,则不会。 这是因为 [] 运算符必须返回对映射中对象的引用,因此它别无选择,只能生成一个(如果尚不存在)。
If you look in the map using the [] operator, then, yes, you will generate default objects. If you use 'find', on the other hand, it does not. This is because the [] operator MUST return a reference to an object in the map, so it has no choice but to generate one if one is not already there.
这背后的原因是:
[] 被定义为
引用永远不能为 NULL,因此必须返回某个值。 STL 通过插入默认的初始化元素来解决这个问题。
The reasoning behind this is:
[] is defined as
A Reference can never be NULL, so some value must be returned. STL solves this by inserting a default initialized element.
这里映射检查键“some key”是否已经存在于映射中:
返回“Some key”对应的值。
键“Some Key”以默认值插入到地图中。
新插入值的引用将是
回。
测试某个key是否存在于map中(不将key添加到map中)的正确方法是:
Here map checks to see if the key "some key" is already exists the map:
the value corresponding to the "Some key" is returned.
key "Some Key" is inserted in the map with default value.
reference of the newly inserted value will be
returned.
The correct way of testing whether a key exits in map (without adding the key to map) is:
怎么搜啊???
这是不正确的,一旦您通过运算符[]访问地图,就会创建适当的位置并返回引用。
你需要使用
How do you search???
This is not correct, once you access a map via operator[] the apropriate place is created and the reference is returned.
You need to use
听起来您正在使用括号运算符,即
不要这样做。 相反,请使用map::find。
如果键不存在,括号运算符会自动将键插入到映射中,并使用值部分的默认值。
It sounds like you're using the bracket operator, i.e.
Don't do that. Instead, use map::find.
The bracket operator will automatically insert the key into the map if it doesn't exist, using the default for the value part.
如果通过搜索您的意思是使用运算符[],如下所示:
那么是的,如果“foo”尚不存在,那么将为“foo”创建一个条目。 因此,您通常应该避免对映射使用operator[],而是使用命名函数sfind() 和insert()。
至于在哪里可以找到有关此行为的信息,关于标准库的最好的书是 Nicolai 的 C++ 标准库乔苏蒂斯。
If by search you mean using operator[], like this:
then yes, that will create an entry for "foo" if it does not already exist. For this reason, you should generally avoid using operator[] for maps and use the named function sfind() and insert() instead.
As for where to find out information on this behaviour, the best book on the standard library is The C++ Standard Library by Nicolai Josuttis.