gpt4 book ai didi

c++ - 如何查看指针映射中的指针是否不为 0

转载 作者:可可西里 更新时间:2023-11-01 11:13:53 31 4
gpt4 key购买 nike

我制作了一张 map ,我需要在另一个线程中使用该 map ,所以我制作了一个指向我的 map 的指针并将其发送到另一个线程。但是,当我想查看映射中的值是否不为 NULL(指针)时,我得到了一个错误。

这是一个示例代码:

#include <iostream>
#include <vector>
#include <map>

using namespace std;


int main()
{
int test = 1;

map<int,void *> handle;
map<int,void *> * handle2;

handle[0] = &test;

handle2 = &handle;


if(*handle2[0])
{
cout << "Works\n";
}

system("Pause");


return false;
}

这是我得到的错误:

错误 C2451:'std::map<_Kty,_Ty>' 类型的条件表达式是非法的

在这种情况下,我如何欺骗 0 指针?

最佳答案

handle2 是一个指向 map 的指针,所以 handle2[0](等同于 *handle2)是 map 本身。如错误所述,这不能用作条件表达式。

如果要检查handle2是否为null,只要说handle2;对于键为 0 的 map 元素,您需要 (*handle2)[0]

关于c++ - 如何查看指针映射中的指针是否不为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9316771/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com