c++ STL 映射复制将布尔值设置为 true

发布于 2024-09-27 23:41:12 字数 646 浏览 2 评论 0原文

我的 stl 地图有问题。最初,我用这样的数据填充地图。

//loop
pair< int, int > xy (x,y);
currentMap.insert( make_pair(xy), value); //map< pair<int, int>, bool>
prevMap.insert( make_pair(xy), value);
// End Loop

然后我根据一些规则删除一个元素,如下所示。

currentMap.erase( make_pair(xy) );

后来我交换了两个地图对象。

prevMap = currentMap;

交换后,所有元素都被分配为 true。如果我搜索元素 xy... *编辑-我领先了。对不起。

i = currentMap.find( make_pair(xy) );
return i->second; // Always true after swap.

真理总是会被评估为真理。初始化地图会将 bool 值设置为 true 吗?我可以用 false 来初始化所有 bool 吗?

谢谢。

I'm having a problem with stl map. Initially I fill the map with data like so.

//loop
pair< int, int > xy (x,y);
currentMap.insert( make_pair(xy), value); //map< pair<int, int>, bool>
prevMap.insert( make_pair(xy), value);
// End Loop

Then I delete an element according to some rules like so.

currentMap.erase( make_pair(xy) );

I later do a swap of the two map objects.

prevMap = currentMap;

After the swap all of the elements are assigned to true. If I search for element xy...
*edit - i got a ahead of myself. Sorry.

i = currentMap.find( make_pair(xy) );
return i->second; // Always true after swap.

truth will always evaluate to true. Will initializing a map set the bool value to true? Can I initialise with all bools at false.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

静谧 2024-10-04 23:41:12

您无法将地图的值初始化为任何值。您只能将值与键一起插入。如果您想在密钥丢失时返回 false,您的最后一个代码片段应该如下所示:

i = currentMap.find( make_pair(xy) );
if (i != currentMap.end())
    return i->second;
return false;

You can't initialize the map's values to anything. You can only insert values along with the key. Your last code snippet should look like this if you want to return false when the key is missing:

i = currentMap.find( make_pair(xy) );
if (i != currentMap.end())
    return i->second;
return false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文