2013-01-07 35 views
1
if (mySharedState -> liveIPs -> find(flowStats -> destinationIP) != mySharedState -> liveIPs -> end()){ 
    //do something 
} 

unordered_map <uint32_t, std::string> *liveIPs;指针和方法的使用奇怪

我从来没有见过这样的用法(找到(...)结束()的使用)。有人能帮助我了解它返回的内容吗? (这是C++的方式代码)

+4

阅读[文件](http://en.cppreference.com/w/cpp/container/map/find),你就会明白为什么。 – chris

+0

我可以看到什么查找方法。你发的不是我要求的? – smttsp

回答

4

您可以使用此技术来检查,如果容器包含的价值。

find()返回对应于该值的迭代器,end()返回容器末端后的迭代器1,该容器用于发出“未找到值”的信号。

+0

是的,我可以看到,但我要求它返回什么,一个布尔值? – smttsp

+1

@tusherity再次阅读:“找到()返回一个迭代器** **” – WhozCraig

+0

@tusherity它本质上是一个'= B'返回一个布尔值! (a == find(),b == end()) –

1

函数找到(值)和结束()是用于调用存储各种类型(名单,集,向量,地图...)的元素“容器”的类的成员函数。有更多关于容器的信息here

两个成员函数返回一个迭代(样的指针)的容器元素。你可以阅读迭代器here

抽象地讲,发现(值)会给你一个容器,它等于值的元素的位置。 end()将返回一个迭代器指向容器的末尾(最后一个元素后面的位置)。

所以你的情况:

// from mSharedState get liveIPs (a container storing IPs) 
// and find the element with value destinationIP 
mSharedState->liveIPs->find(flowStats->destinationIP) 

// check if the iterator returned by find(flowStats->destinationIP) is different 
// then the end of the liveIPs contatiner 
!= liveIPs->end() 

所以,“//做一些事情”将如果容器liveIPs保存与价值destinationIP元素执行。因为find(value)和end()通常是容器的成员函数,所以我认为你显示的代码片段是STL一致容器的成员函数的定义的一部分(可能是一些用户定义的容器符合STL容器接口,提供find(value)和end()作为成员函数)。