C++ 遍历 Map

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26281979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:33:44  来源:igfitidea点击:

C++ Loop through Map

c++dictionary

提问by NoName

I want to iterate through each element in the map<string, int>without knowing any of its string-int values or keys.

我想在map<string, int>不知道任何 string-int 值或键的情况下遍历中的每个元素。

What I have so far:

到目前为止我所拥有的:

void output(map<string, int> table)
{
       map<string, int>::iterator it;
       for (it = table.begin(); it != table.end(); it++)
       {
            //How do I access each element?  
       }
}

回答by P0W

You can achieve this like following :

您可以像下面这样实现:

map<string, int>::iterator it;

for ( it = symbolTable.begin(); it != symbolTable.end(); it++ )
{
    std::cout << it->first  // string (key)
              << ':'
              << it->second   // string's value 
              << std::endl ;
}


With C++11( and onwards ),

使用C++11 (及以后)

for (auto const& x : symbolTable)
{
    std::cout << x.first  // string (key)
              << ':' 
              << x.second // string's value 
              << std::endl ;
}


With C++17( and onwards ),

使用C++17 (及以后)

for( auto const& [key, val] : symbolTable )
{
    std::cout << key         // string (key)
              << ':'  
              << val        // string's value
              << std::endl ;
}

回答by Vlad from Moscow

Try the following

尝试以下

for ( const auto &p : table )
{
   std::cout << p.first << '\t' << p.second << std::endl;
} 

The same can be written using an ordinary for loop

可以使用普通的 for 循环编写相同的内容

for ( auto it = table.begin(); it != table.end(); ++it  )
{
   std::cout << it->first << '\t' << it->second << std::endl;
} 

Take into account that value_type for std::mapis defined the following way

考虑到 value_type forstd::map的定义方式如下

typedef pair<const Key, T> value_type

Thus in my example p is a const reference to the value_type where Key is std::stringand T is int

因此,在我的示例中,p 是对 value_type 的常量引用,其中 Key 是std::string,T 是int

Also it would be better if the function would be declared as

如果将函数声明为

void output( const map<string, int> &table );

回答by Columbo

The value_typeof a mapis a paircontaining the key and value as it's firstand secondmember, respectively.

所述value_typemap是一种pair含有该键和值,因为它的firstsecond分别构件。

map<string, int>::iterator it;
for (it = symbolTable.begin(); it != symbolTable.end(); it++)
{
    std::cout << it->first << ' ' << it->second << '\n';
}

Or with C++11, using range-based for:

或者在 C++11 中,使用基于范围的:

for (auto const& p : symbolTable)
{
    std::cout << p.first << ' ' << p.second << '\n';
}

回答by John Mutuma

As @Vlad from Moscow says, Take into account that value_typefor std::mapis defined the following way:

正如来自莫斯科的@Vlad 所说,考虑到value_typeforstd::map定义如下:

typedef pair<const Key, T> value_type

This then means that if you wish to replace the keyword autowith a more explicit type specifier, then you could this;

这意味着如果您希望auto用更明确的类型说明符替换关键字,那么您可以这样做;

for ( const pair<const string, int> &p : table ) {
   std::cout << p.first << '\t' << p.second << std::endl;
} 

Just for understanding what autowill translate to in this case.

只是为了理解auto在这种情况下会转化为什么。