Iterating over the keys in map without using boost library

Hi,

I aware of iterating both keys and values in maps.But how to iterate the map keys in c++ without using boost library.

python:

for key in sorted(dict_1.keys()):
for i in range(0,len(key)): //what is the equivalent for loop in c++.
print("x")

c++:

for(auto itr = dict_1.begin(); itr!= dict_1.end(); ++itr){
What is the equivalent c++ for loop code to loop through the dict_1 keys.
}

Thank you for your suggestions.

Thanks,
Shruthi
Last edited on
1
2
3
4
for(auto itr = dict_1.begin(); itr!= dict_1.end(); ++itr)
{
  itr->first; // THIS IS THE KEY
}



Can anyone debug this code snippet. Thank you!

std::map<std::tuple<int,int,int,int>,std::array<char,3> mp;

mp.insert({{0,0,1,0},{'a','b','c'}});

mp.insert({{1,1,1,1},{'d','e','f'}});

for(auto itr = mp.begin(); itr!= mp.end(); ++itr){
std::cout<<itr->first;
}

I am getting error at cout. Not able to print the cout.

Error says: "no operator \"<<\" matches these operands -- operand types are: std::basic_ostream<char, std::char_traits<char>> << const std::tuple<int, int, int, int>"

Your problem is not in map, keys, or iteration. It is in printing tuple:
1
2
3
4
5
6
7
8
9
// my first program in C++
#include <iostream>
#include <tuple>

int main()
{
  std::tuple<int,int,int,int> example {1,3,2,0};
  std::cout << example; // error
}

error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}'
  and 'std::tuple<int, int, int, int>')

std::tuple does not have rich interface. http://www.cplusplus.com/reference/tuple/tuple/

You have to be explicit:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <tuple>

int main()
{
  std::tuple<int,int,int,int> example {1,3,2,0};
  std::cout << std::get<0>(example) << ' '
    << std::get<1>(example) << ' '
    << std::get<2>(example) << ' '
    << std::get<3>(example) << '\n';
}

1 3 2 0
Last edited on
For anyone using C++11 or newer, the above answer from Repeater can be further improved using the range-for statement (see https://en.cppreference.com/w/cpp/language/range-for for more details).

So-called const correctness should also be observed.

An example is provided below.

1
2
3
for (const auto& kv_pair : my_map) {
    std::cout << kv_pair.first << " has value " << kv_pair.second << std::endl;
}


The use of range-for statements over for statements (where a choice is possible) is recommended within ES.71 of the ISO CPP Core Guidelines (see https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es71-prefer-a-range-for-statement-to-a-for-statement-when-there-is-a-choice ).

The use of appropriate const correctness is recommended within a whole section of the Guidelines titled 'Con: Constants and immutability'. Guideline Con.1 recommends making objects constant by default if the value of the object is immutable (see https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rconst-immutable ).
Thank you all for the replies.
Topic archived. No new replies allowed.