-1

我有一段代码要理解。但是我迷失了一点。这是代码:

typedef unordered_map <string, TimeStampSet *> HIEMap;
typedef set <TimeStamp> TimeStampSet;

struct HostInfo {
    HostActivity *hostActivity;
    HIEMap *hieMapArr;
};

typedef unordered_map <uint32_t, HostInfo *> HostInfoMap;

HIEMap::iterator hieMapIt;

void method(...){
    for (hieMapIt = hostInfoIt -> second -> hieMapArr -> begin();
         hieMapIt != hostInfoIt -> second -> hieMapArr -> end();
         hieMapIt = nextMapIt)
    {
        if (hieMapIt -> second == NULL) {
           //what does *hieMapIt -> second* returns?
        }
    }
}

返回什么hieMapIt -> second?我有点迷路了。

这不是所有的代码,还有初始化等等。但我没有把所有的代码都放在这里。

谢谢,

4

3 回答 3

5

它将返回一个TimeStampSet *. 即 unordered_map (string , TimeStampSet *) "pair" 的第二部分。

于 2013-01-20T14:27:19.027 回答
2

Dereferencing an iterator of a container gives you an object whose type is the value_type of the container. For a map<K, T> (or unordered_map), the value_type is pair<K const, T>, so that you can use it->first and it->second to access the key and mapped value of the map element.

So hostInfoIt->second is a HostInfo *, ...->hieMapArr is a HIEMap *, and hieMapIt is a HIEMap::iterator. Thus hieMapIt->second is a TimeStampSet *.

于 2013-01-20T14:31:56.893 回答
1

一个std::unordered_map<K,V>contains std::pair<const K,V>,因此将迭代器取消引用到此类 map 元素的元素可为您提供其中一个的句柄。Andstd::pair<const K,V>::second是 a V,在您的情况下是指向TimeStampSet

于 2013-01-20T14:27:38.683 回答