Answers for "c++ map iterator"

C++
8

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
Posted by: Guest on April-30-2020
2

c++ map iterator

#include <iostream>
#include <map>
 
int main() {
  std::map<int, float> num_map;
  // calls a_map.begin() and a_map.end()
  for (auto it = num_map.begin(); it != num_map.end(); ++it) {
    std::cout << it->first << ", " << it->second << '\n';
  }
}
Posted by: Guest on March-08-2020
0

iterate through map c++

for (auto i : m)
        cout << i.first << "   " << i.second
             << endl;
Posted by: Guest on July-22-2021

Browse Popular Code Answers by Language