确定映射是否包含键的值?

2024-05-11C/C++开发问题
7

本文介绍了确定映射是否包含键的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

确定 STL 映射是否包含给定键的值的最佳方法是什么?

What is the best way to determine if a STL map contains a value for a given key?

#include <map>

using namespace std;

struct Bar
{
    int i;
};

int main()
{
    map<int, Bar> m;
    Bar b = {0};
    Bar b1 = {1};

    m[0] = b;
    m[1] = b1;

    //Bar b2 = m[2];
    map<int, Bar>::iterator iter = m.find(2);
    Bar b3 = iter->second;

}

在调试器中检查这个,看起来 iter 只是垃圾数据.

Examining this in a debugger, it looks like iter is just garbage data.

如果我取消注释这一行:

If I uncomment out this line:

Bar b2 = m[2]

调试器显示 b2{i = 0}.(我猜这意味着使用未定义的索引将返回一个包含所有空/未初始化值的结构?)

The debugger shows that b2 is {i = 0}. (I'm guessing it means that using an undefined index will return a struct with all empty/uninitialized values?)

这些方法都不是很好.我真正想要的是这样的界面:

Neither of these methods is so great. What I'd really like is an interface like this:

bool getValue(int key, Bar& out)
{
    if (map contains value for key)
    {
        out = map[key];
        return true;
    }
    return false;
}

是否存在类似这样的东西?

Does something along these lines exist?

推荐答案

是否存在类似这样的东西?

Does something along these lines exist?

没有.使用 stl 映射类,您可以使用 ::find() 搜索地图,并将返回的迭代器与 std::map::end()

No. With the stl map class, you use ::find() to search the map, and compare the returned iterator to std::map::end()

所以

map<int,Bar>::iterator it = m.find('2');
Bar b3;
if(it != m.end())
{
   //element found;
   b3 = it->second;
}

显然,如果您愿意,您可以编写自己的 getValue() 例程(同样在 C++ 中,没有理由使用 out),但我怀疑一旦你掌握了使用 std::map::find() 的窍门,你不会想浪费时间.

Obviously you can write your own getValue() routine if you want (also in C++, there is no reason to use out), but I would suspect that once you get the hang of using std::map::find() you won't want to waste your time.

还有你的代码有点错误:

m.find('2'); 将在地图中搜索 '2' 的键值.IIRC C++ 编译器会将 '2' 隐式转换为 int,这会导致 '2' 的 ASCII 代码的数值不是您想要的.

m.find('2'); will search the map for a keyvalue that is '2'. IIRC the C++ compiler will implicitly convert '2' to an int, which results in the numeric value for the ASCII code for '2' which is not what you want.

因为你在这个例子中的keytype是int,你想这样搜索:m.find(2);

Since your keytype in this example is int you want to search like this: m.find(2);

这篇关于确定映射是否包含键的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6