在C ++ 11中的每次循环迭代后更新映射

时间:2015-12-06 06:42:24

标签: c++ c++11

我有一个循环,它正在计算p的第二个成员的大小,

其中pstd::pair<const std::string, std::set<std::string>>

for (const auto& p : ref)
    std::cout << p.second.size() << endl;

现在我要创建另一个地图cntstd::map<std::string, int> cnt;), 每次迭代后保存p.firstp.second.size()

我该怎么做?

1 个答案:

答案 0 :(得分:2)

声明std::map<std::string, int> cnt; 在迭代用于存储对的任何数据结构之前。虽然您正在迭代此结构,但只需输入

即可
cnt[p.first] = p.second.size();