带有指针的嵌套 foreach 循环?

Nested foreach loop with pointers?

本文关键字:foreach 循环 嵌套 指针      更新时间:2023-10-16
map<string, vector<int>*> settings
for (auto el : settings)
{
for(auto i : el)
{
cout << i;
}
}

我在 el 内部得到:这个基于范围的"for"语句需要一个合适的开始函数,但没有找到。 我该如何解决这个问题?

当您使用

map<string, vector<int>*> settings
for (auto el : settings)
{
}

el是一个std::pair<const string, vector<int>*>.在 cppreference.com 查看std::map::value_type的定义。

要从向量中获取项目,您需要使用

map<string, vector<int>*> settings
for (auto el : settings)
{
for ( auto item : *(el.second) )
{
// Use item
}
}

为避免不必要的复制std::pair,可以使用auto const& el

map<string, vector<int>*> settings
for (auto const& el : settings)
{
for ( auto item : *(el.second) )
{
// Use item
}
}