STL映射存储搜索的键

发布于 2024-07-19 03:01:45 字数 317 浏览 1 评论 0原文

我刚刚发现,当我搜索这样的地图时:

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

妄司 2024-07-26 03:01:45

您是否使用 [] 运算符进行搜索? 如果是这样,那么是的,这就是定义的行为。

如果您不希望出现此行为,则应该使用“查找”方法。

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.

作死小能手 2024-07-26 03:01:45

如果您使用 [] 运算符查看地图,那么,是的,您将生成默认对象。 另一方面,如果您使用“查找”,则不会。 这是因为 [] 运算符必须返回对映射中对象的引用,因此它别无选择,只能生成一个(如果尚不存在)。

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.

倥絔 2024-07-26 03:01:45

这背后的原因是:
[] 被定义为

T& operator[](KEY k)

引用永远不能为 NULL,因此必须返回某个值。 STL 通过插入默认的初始化元素来解决这个问题。

The reasoning behind this is:
[] is defined as

T& operator[](KEY k)

A Reference can never be NULL, so some value must be returned. STL solves this by inserting a default initialized element.

撕心裂肺的伤痛 2024-07-26 03:01:45
int a = aMap["some key"];

这里映射检查键“some key”是否已经存在于映射中:

  • 如果是,则引用
    返回“Some key”对应的值。
  • 如果地图中不存在“某个键”,则
    键“Some Key”以默认值插入到地图中。
    新插入值的引用将是
    回。

测试某个key是否存在于map中(不将key添加到map中)的正确方法是:

std::map<key,value>::iterator iter = myMap.find("Some Key");
if( iter != myMap.end())
{
 //key exists
}
else
{
 //no key
}
int a = aMap["some key"];

Here map checks to see if the key "some key" is already exists the map:

  • if yes then the reference of
    the value corresponding to the "Some key" is returned.
  • If "some key" doesnot exists in the map then the
    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:

std::map<key,value>::iterator iter = myMap.find("Some Key");
if( iter != myMap.end())
{
 //key exists
}
else
{
 //no key
}
梦里南柯 2024-07-26 03:01:45

怎么搜啊???

if(!aMap[key]) // not found

这是不正确的,一旦您通过运算符[]访问地图,就会创建适当的位置并返回引用。

你需要使用

if(aMao.find(key)==aMap.end()) // not found

How do you search???

if(!aMap[key]) // not found

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

if(aMao.find(key)==aMap.end()) // not found
短暂陪伴 2024-07-26 03:01:45

听起来您正在使用括号运算符,即

if (aMap["string"] == something)

不要这样做。 相反,请使用map::find。

如果键不存在,括号运算符会自动将键插入到映射中,并使用值部分的默认值。

It sounds like you're using the bracket operator, i.e.

if (aMap["string"] == something)

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.

月下伊人醉 2024-07-26 03:01:45

如果通过搜索您的意思是使用运算符[],如下所示:

if ( m["foo"] == 42 ) {
  // found
}
else {
  // not
}

那么是的,如果“foo”尚不存在,那么将为“foo”创建一个条目。 因此,您通常应该避免对映射使用operator[],而是使用命名函数sfind() 和insert()。

至于在哪里可以找到有关此行为的信息,关于标准库的最好的书是 Nicolai 的 C++ 标准库乔苏蒂斯

If by search you mean using operator[], like this:

if ( m["foo"] == 42 ) {
  // found
}
else {
  // not
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文