How can I print out C++ map values?
261,264
Solution 1
for(map<string, pair<string,string> >::const_iterator it = myMap.begin();
it != myMap.end(); ++it)
{
std::cout << it->first << " " << it->second.first << " " << it->second.second << "\n";
}
In C++11, you don't need to spell out map<string, pair<string,string> >::const_iterator
. You can use auto
for(auto it = myMap.cbegin(); it != myMap.cend(); ++it)
{
std::cout << it->first << " " << it->second.first << " " << it->second.second << "\n";
}
Note the use of cbegin()
and cend()
functions.
Easier still, you can use the range-based for loop:
for(const auto& elem : myMap)
{
std::cout << elem.first << " " << elem.second.first << " " << elem.second.second << "\n";
}
Solution 2
If your compiler supports (at least part of) C++11 you could do something like:
for (auto& t : myMap)
std::cout << t.first << " "
<< t.second.first << " "
<< t.second.second << "\n";
For C++03 I'd use std::copy
with an insertion operator instead:
typedef std::pair<string, std::pair<string, string> > T;
std::ostream &operator<<(std::ostream &os, T const &t) {
return os << t.first << " " << t.second.first << " " << t.second.second;
}
// ...
std:copy(myMap.begin(), myMap.end(), std::ostream_iterator<T>(std::cout, "\n"));
Solution 3
Since C++17 you can use range-based for loops together with structured bindings for iterating over your map. This improves readability, as you reduce the amount of needed first
and second
members in your code:
std::map<std::string, std::pair<std::string, std::string>> myMap;
myMap["x"] = { "a", "b" };
myMap["y"] = { "c", "d" };
for (const auto &[k, v] : myMap)
std::cout << "m[" << k << "] = (" << v.first << ", " << v.second << ") " << std::endl;
Output:
m[x] = (a, b)
m[y] = (c, d)
Author by
Zhi Rui
Updated on July 06, 2022Comments
-
Zhi Rui almost 2 years
I have a
map
like this:map<string, pair<string,string> > myMap;
And I've inserted some data into my map using:
myMap.insert(make_pair(first_name, make_pair(middle_name, last_name)));
How can I now print out all the data in my map?
-
Manjunath over 6 yearsWhy is the & required in the above snipped?
for (auto& t : myMap) std::cout << t.first << " " << t.second.first << " " << t.second.second << "\n";
-
CyprUS over 6 yearsUsing a reference
&
avoids making a copy during the loop iteration. -
andreee over 5 yearsGood update! You might want to take your parameters as const reference in the for loop.
-
AlphaFoxtrot almost 4 yearsWhen I try doing the for loop, I get "identifier "k" is undefined."
-
honk almost 4 years@AlphaFoxtrot: Did you really compile it for C++17? As you can see from the link to Coliru, the code is actually working.
-
AlphaFoxtrot almost 4 yearsAh I see. I recently downloaded Visual Studio and I thought it would put me on the latest. Turns out I was using C++98.
-
Paul almost 3 yearsisn't the last one slower?
-
Armen Tsirunyan almost 3 years@Paul: I edited it to to
const auto&
to avoid the copying of the elements. Hope that is what you meant. Other than this, no, the last one is not slower