C++ Loop through Map

前端 未结 5 639
抹茶落季
抹茶落季 2020-11-30 16:56

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

What I have so far:



        
相关标签:
5条回答
  • 2020-11-30 17:22

    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 ( 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::map is defined the following way

    typedef pair<const Key, T> value_type
    

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

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

    void output( const map<string, int> &table );
    
    0 讨论(0)
  • 2020-11-30 17:25

    As P0W has provided complete syntax for each C++ version, I would like to add couple of more points by looking at your code

    • Always take const & as argument as to avoid extra copies of the same object.
    • use unordered_map as its always faster to use. See this discussion

    here is a sample code:

    #include <iostream>
    #include <unordered_map>
    using namespace std;
    
    void output(const auto& table)
    {
       for (auto const & [k, v] : table)
       {
            std::cout << "Key: " << k << " Value: " << v << std::endl;
       }
    }
    
    int main() {
        std::unordered_map<string, int> mydata = {
            {"one", 1},
            {"two", 2},
            {"three", 3}
        };
        output(mydata);
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-30 17:30

    The value_type of a map is a pair containing the key and value as it's first and second member, respectively.

    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:

    for (auto const& p : symbolTable)
    {
        std::cout << p.first << ' ' << p.second << '\n';
    }
    
    0 讨论(0)
  • 2020-11-30 17:31

    As @Vlad from Moscow says, Take into account that value_type for std::map is defined the following way:

    typedef pair<const Key, T> value_type
    

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

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

    Just for understanding what auto will translate to in this case.

    0 讨论(0)
  • 2020-11-30 17:47

    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 ),

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

    With C++17 ( and onwards ),

    for( auto const& [key, val] : symbolTable )
    {
        std::cout << key         // string (key)
                  << ':'  
                  << val        // string's value
                  << std::endl ;
    }
    
    0 讨论(0)
提交回复
热议问题