Horje
how to iterate through a map in c++ Code Example
how to iterate through a map in c++
//traditional way (long)
for(map<string,int>::iterator it=m.begin(); it!=m.end(); ++it)
	if(it->second)cout<<it->first<<" ";
//easy way(short) just works with c++11 or later versions
for(auto &x:m)
	if(x.second)cout<<x.first<<" ";
//condition is just an example of use
c++ iterate map
// C++11 and onwards
for (auto const& keyValue : map)
{
  keyValue.first; // Key
  keyValue.second; // Value
}
c++ map loop through key value
#include <iostream>
#include <map>

int main() {
    std::map<std::string, int> myMap;

    myMap["one"] = 1;
    myMap["two"] = 2;
    myMap["three"] = 3;

    for ( const auto &[key, value]: myMap ) {
        std::cout << key << '\n';
    }
}




Cpp

Related
eosio multi index secondary index Code Example eosio multi index secondary index Code Example
vector with pinter cout c++ Code Example vector with pinter cout c++ Code Example
fibonacci series in c++ Recursive Code Example fibonacci series in c++ Recursive Code Example
unordered_map of pair and int Code Example unordered_map of pair and int Code Example
initialize vector to all zeros c++ Code Example initialize vector to all zeros c++ Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
8