我有一个循环,它正在计算p
的第二个成员的大小,
其中p
为std::pair<const std::string, std::set<std::string>>
for (const auto& p : ref)
std::cout << p.second.size() << endl;
现在我要创建另一个地图cnt
(std::map<std::string, int> cnt;
),
每次迭代后保存p.first
和p.second.size()
。
我该怎么做?
答案 0 :(得分:2)
声明std::map<std::string, int> cnt;
在迭代用于存储对的任何数据结构之前。虽然您正在迭代此结构,但只需输入
cnt[p.first] = p.second.size();