I'm trying to print all the content of an std::unordered_map specified like this:
std::unordered_map<uint64_t, std::unordered_map<uint64_t,uint64_t>> m;
After adding things in the map, I tried the following:
for (auto it=map.begin(); it!=map.end(); it++) {
cout << it->first << it->second << endl;
}
but it is not working.
Since you have nested std::unordered_map, following should work:
for (auto const& i : m) {
for (auto const& j : i.second) {
std::cout << j.first << " " << j.second << std::endl;
}
}
You have to iterate over the nested map as well. And when you work with maps it is very convenient to use a range-based for on top of a structured binding. To avoid these cryptic first and second things:
for (const auto& [key1, value1] : map)
for (const auto& [key2, value2] : value1)
std::cout << key2 << " " << value2 << std::endl;
It works only in C++17 though. If you cannot use it, then you have the answer by NutCracker.
How to print the content of a nested std::unordered_map?
To print nested std::unordered_map use nested range-based for loop.
for (auto const& i: m) {
std::cout << "Key: " << i.first << " (";
for (auto const& j: i.second)
std::cout << j.first << " " << j.second;
std::cout << " )" << std::endl;
}
However, if you want to modify the container's elements:
for (const& i: m) {
for (const& j: i.second)
// Do operations
}
Related
First one being:
map <int,int> m;
//... some elements inserted
auto i= m.begin();
cout<<(*i).first<<(*i).second;
Here we are required to use the dereference operator *
Second:
map <int,int> m;
//... some elements inserted
for(auto i: m)
cout<<i.first<<i.second;
Why am I not required to use the * operator this time?
One more doubt:
for(auto &i: m)
what difference does '&' make here?
auto i=m.begin() will give you iterator .. which is accessed more like a pointer (syntactically) when you want to access the value...
for(auto i:m) will copy current element of m (a pair) into i , i is a copy of element, not the element itself...
for (auto &i: m) will work on a reference, the original map is affected
As explained in the below code snippet, the first i is of type iterator where as the i in the for loops are of pair type.
#include <iostream>
#include <map>
int main()
{
std::map <int,int> m;
m[1] = 5;
m[10] = 60;
m[100] = 800;
// Below i is map iterator (std::map<int, int>::iterator)
auto i = m.begin();
std::cout << typeid(i).name() << '\n';
std::cout << (*i).first << " : " << (*i).second << '\n';
std::cout << i->first << " : " << i->second << '\n';
for(auto i: m) {
// Below i is pair<int, int>
std::cout << typeid(i).name() << '\n';
std::cout << i.first << " : " << i.second << '\n';
}
for(auto& i: m) {
// Below i is reference of pair<int, int>)
// modifying this would result in updated values in the map.
std::cout << typeid(i).name() << '\n';
std::cout << i.first << " : " << i.second << '\n';
}
return 0;
}
I've got multiple containers of different types.
I'd like to perform the same action on all of the elements across all the containers.
Normally, this involves multiple range-based for loops with duplicated code:
#include <iostream>
#include <vector>
#include <set>
int main() {
//containers
std::vector<int> v1{1,2,3,4,5};
std::set<float> v2{6,7,8,9,10};
//perform iterations
for (auto & it: v1){
std::cout << it << ' ';
}
for (auto & it: v2){
std::cout << it << ' ';
}
}
I'd like to be able to do this instead,
by providing multiple containers to the same range-based for loop.
This of course does not work:
for (auto & it: v1,v2){
std::cout << it << ' ';
}
Is there a library solution I could use to achieve this?
You could use boost range's combine:
for(auto&& el : boost::combine(v1, v2)) {
std::cout << boost::get<0>(el) << ", " << boost::get<1>(el) << '\n';
}
demo
Or, range-v3's zip view:
for(auto&& el : view::zip(v1, v2)) {
std::cout << std::get<0>(el) << ", " << std::get<1>(el) << '\n';
}
demo
Alternatively, you could make a range from zip iterators the hard way:
auto b = boost::make_zip_iterator(boost::make_tuple(v1.begin(), v2.begin()));
auto e = boost::make_zip_iterator(boost::make_tuple(v1.end(), v2.end()));
for(auto&& tup : boost::make_iterator_range(b, e)) {
std::cout << boost::get<0>(tup) << ", " << boost::get<1>(tup) << '\n';
}
demo
std::map<int, std::map<std::string, std::pair<int, std::string>>>mfvs;
I want to output the std::string the first part of inner map.
for (const auto& iter :mfvs)
{
std::cout << iter.second.first << "\n";
}
this gives me an error
iter.second is an std::map, and as such it doesn't have first.
If you want to print the first (with lowest key) element (and you are sure it exists), then do this:
std::cout << iter.second.begin()->first << "\n";
.begin() will return you the first element of the map, and ->first will get its key. If you want to print the value of it as well, something along the lines of the following code will work
auto firstElem = iter.second.begin();
std::cout << firstElem->first << ": " << firstElem->second.first << ", " << firstElem->second.second << "\n";
I am trying to use the unordered_map in C++, such that, for the key I have an int, while for the value there is a pair of floats. But, I am not sure how to access the pair of values. I am just trying to make sense of this data structure. I know to access the elements we need an iterator of the same type as this unordered map declaration. I tried using iterator->second.first and iterator->second.second. Is this the correct way to do access elements?
typedef std::pair<float, float> Wkij;
tr1::unordered_map<int, Wkij> sWeight;
tr1::unordered_map<int, Wkij>:: iterator it;
it->second.first // access the first element of the pair
it->second.second // access the second element of the pair
Thanks for your help and time.
Yes, this is correct, but don't use tr1, write std, since unordered_map is already part of STL.
Use iterators like you said
for(auto it = sWeight.begin(); it != sWeight.end(); ++it) {
std::cout << it->first << ": "
<< it->second.first << ", "
<< it->second.second << std::endl;
}
Also in C++11 you can use range-based for loop
for(auto& e : sWeight) {
std::cout << e.first << ": "
<< e.second.first << ", "
<< e.second.second << std::endl;
}
And if you need it you can work with std::pair like this
for(auto it = sWeight.begin(); it != sWeight.end(); ++it) {
auto& p = it->second;
std::cout << it->first << ": "
<< p.first << ", "
<< p.second << std::endl;
}
I want to iterate through each element in the map<string, int> without knowing any of its string-int values or keys.
What I have so far:
void output(map<string, int> table)
{
map<string, int>::iterator it;
for (it = table.begin(); it != table.end(); it++)
{
//How do I access each element?
}
}
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;
}
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 );
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';
}
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.
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;
}
if you just want to iterate over the content without changing values
do:
for(const auto & variable_name : container_name(//here it is map name)){
cout << variable_name.first << " : " << variable_name.second << endl;
}
If you want to modify the contents of the map, remove the const and keep & (if you want to modify directly the contents inside container). If you want to work with a copy of the container values, remove the & sign too; after that, you can access them by using .first and .second on "variable_name".
it can even be done with a classic for loop.
advancing the iterator manually.
typedef std::map<int, int> Map;
Map mymap;
mymap['a']=50;
mymap['b']=100;
mymap['c']=150;
mymap['d']=200;
bool itexist = false;
int sizeMap = static_cast<int>(mymap.size());
auto it = mymap.begin();
for(int i = 0; i < sizeMap; i++){
std::cout << "Key: " << it->first << " Value: " << it->second << std::endl;
it++;
}
Other way :
map <int, string> myMap = {
{ 1,"Hello" },
{ 2,"stackOverflow" }
};
for (auto iter = cbegin(myMap); iter != cend(myMap); ++iter) {
cout << iter->second << endl;
}