gpt4 book ai didi

c++ - 从c++中的 map 读取集合

转载 作者:行者123 更新时间:2023-11-30 05:38:36 24 4
gpt4 key购买 nike

这是一个非常愚蠢的问题,但不知何故我无法解决。

我有一个映射,它的键是字符串并且设置了值,我想遍历值并打印它,例如

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
}

我的问题是如何遍历 set ?

最佳答案

确实 std::string 没有成员 second

因为你有 c++11,你可以让生活变得更轻松:

std::map<std::string, std::set<std::string>> test_map_set

for (std::string& set_element : test_map_set["test"])
{
}

对于删除:

auto& test = test_map_set["test"];
for (auto it = test.begin(); it!= test.end();)
{
if (it->length()==5)
it = test.erase(it);
else
++it;
}

这将删除所有 5 个字符的字符串

关于c++ - 从c++中的 map 读取集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32662012/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com