[英]How to look if a pointer in a pointer map is not 0
我制作了一个地图,我需要在另一个线程中使用该地图,所以我制作了一个指向我的地图的指针并将其发送到另一个线程。 但是当我想查看映射中的值是否不为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
是指向地图的指针,因此handle2[0]
(相当于*handle2
)是地图本身。 正如错误所言,它不能用作条件表达式。
如果要检查handle2
是否为空,只需说出handle2
; 对于键为0的地图元素,需要(*handle2)[0]
。
if(*handle2[0])
一种可能性:
if((*handle2)[0])
另一个(对我来说有点神秘):
if(handle2[0][0])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.