C++ 带指针的嵌套foreach循环? 地图设置 用于(自动el:设置) { 用于(自动i:el) { 当你使用 map<string, vector<int>*> settings for (auto el : settings) { for(auto i : el) { cout << i; } }

C++ 带指针的嵌套foreach循环? 地图设置 用于(自动el:设置) { 用于(自动i:el) { 当你使用 map<string, vector<int>*> settings for (auto el : settings) { for(auto i : el) { cout << i; } },c++,stl,C++,Stl,为了避免不必要地复制std::pair,可以使用auto-const&el map<string, vector<int>*> settings for (auto el : settings) { for ( auto item : *(el.second) ) { // Use item } } 地图设置 用于(自动常数和el:设置) { 用于(自动项目:*(标高秒)) { //使用项 } } for(auto i:el.second

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

map<string, vector<int>*> settings
for (auto el : settings)
{
   for ( auto item : *(el.second) )
   {
       // Use item
   }
}
地图设置
用于(自动常数和el:设置)
{
用于(自动项目:*(标高秒))
{
//使用项
}
}

for(auto i:el.second)
?每个地图条目都有一个指向向量的指针(这是一个有问题的设计,请考虑智能指针),因此您实际上需要
for(auto i:*(el.second))
el
应该是(const)引用,没有理由复制它data@Slava,有道理。
map<string, vector<int>*> settings
for (auto el : settings)
{
   for ( auto item : *(el.second) )
   {
       // Use item
   }
}
map<string, vector<int>*> settings
for (auto const& el : settings)
{
   for ( auto item : *(el.second) )
   {
       // Use item
   }
}