【发布时间】:2015-09-18 22:33:36
【问题描述】:
这是一个非常愚蠢的问题,但不知何故我无法解决。
我有映射,键是字符串,值已设置,我想遍历值并打印它,例如
std::map<std::string, std::set<std::string>> test_map_set
for (auto it_map = test_map_set["test"].begin(); it_mpa != test_map_set["test"].begin(); it_map ++ )
{
auto it = it_map->second; ===> Here I am getting error that it has no member second
then iterate through set
}
我的问题是如何遍历集合?
【问题讨论】:
-
您已经在迭代集合(映射到“test”),只需取消引用指针以获取该特定集合的元素。如果您想遍历地图,请删除 ["test"],这可能是您想要的。也使用 .end() 代替第二个 .begin()
-
您的
it_map实际上是key = "test"集合的迭代器。所以当然,集合迭代器没有second成员(实际上第二个是一对成员)。
标签: c++ dictionary iterator std