带有指针的嵌套 foreach 循环?
Nested foreach loop with pointers?
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
}
}
相关文章:
- foreach循环中指针到std::unique_ptr的隐式转换
- 带有指针的嵌套 foreach 循环?
- 为什么当数组作为调用方函数中的参数传递时,不能在被调用函数中使用 foreach 循环打印数组的值
- 为什么 vector::clear 在 foreach 循环中不起作用
- 在基于 lambda 的 foreach 循环中模拟 'continue;', 'break;“
- 带有指针的Foreach循环
- 你能使用 Qt foreach 循环迭代每个可能的枚举值吗?
- 使用foreach循环C++更新std::列表中的每个值
- 如何在QSignalSpy上使用foreach循环
- 使用foreach循环嵌套的QHash
- 使用foreach循环修改Qt数据结构的正确方法
- GCC可能存在错误,foreach循环操作的是阴影,而不是实际对象
- C++foreach循环甚至选择可变的begin()
- c++ 11 foreach循环中指向链接的链接
- Qt foreach循环排序与QList的for循环排序
- 在c++ 11 foreach循环中获取index
- c++中二维数组中的foreach循环
- 带有指针而不是引用的c++foreach循环
- 在这个例子中,QT 中的 foreach 循环不起作用
- 在C++中使用 foreach 循环