I have the following map;
std::map<long, std::vector<std::pair<long,long>>> G;
I'm having trouble figuring out how to print the pair values in it, could anyone help me out?
Try a loop:
for (const auto & p : G)
{
std::cout << p.first << " => {";
for (const auto & q : p.second)
std::cout << "(" << q.first << ", " << q.second << ")";
std::cout << "}\n";
}
Related
The following static initializer list M fails but N suceeds, it seems I cannot figure out how to do the unordered_map initialization when a union of 3 const char * pointers is added to the init-list. I tried the . notation to access the fields of the union and the union itself to no avail.
#include <iostream>
#include <unordered_map>
#include <iterator>
struct C {
int j;
int i;
};
struct D {
int j;
char c;
};
struct E {
const char *a;
const char *b;
const char *c;
}e;
struct A {
const char *s;
union {
C c;
D d;
E e;
} u;
};
std::unordered_map<unsigned int, A> m ({
{1, {"tom",53,'o'}},
{2, {"ming",40,41}},
{3, {"peter","a","b","c"}} });
std::unordered_map<unsigned int, A> n ({
{1, {"tom",53,'o'}},
{2, {"ming",40,41}} });,
int main(void) {
for ( const auto& i : m) {
const auto& j = i.second;
if (i.first == 1)
std::cout << "key: " << i.first << " value: " << j.u.d.j << ":" << j.s << ":" << j.u.d.c << std::endl;
else if (i.first == 2)
std::cout << "key: " << i.first << " value: " << j.u.c.j << ":" << j.s << ":" << j.u.c.i << std::endl;
else if (i.first == 3)
std::cout << "key: " << i.first << " value: " << j.u.e.a << ":" << j.s << ":" << j.u.e.c << std::endl;
}
for ( const auto& i : n) {
const auto& j = i.second;
if (i.first == 1)
std::cout << "key: " << i.first << " value: " << j.u.d.j << ":" << j.s << ":" << j.u.d.c << std::endl;
else if (i.first == 2)
std::cout << "key: " << i.first << " value: " << j.u.c.j << ":" << j.s << ":" << j.u.c.i << std::endl;
}
return 0;
}
Error:
map.cpp:41:32: error: no matching function for call to ‘std::unordered_map::unordered_map()’
map<string, int> M;
for (auto E: M)
{
cout << E.first << ": " << E.second << endl;
F << E.first << ": " << E.second << endl;
};
I am learning c++ and I am confused with auto. I am trying to convert the above code into the following code( the above code with auto works correctly)
map<string, int> M;
for (map<string, int> :: iterator p = begin(M); p != end(M); p ++ )
{
cout << p.first << ": " << p.second << endl;
F << p.first << ": " << p.second << endl;
}
I got the following error:
error: ‘std::map<std::basic_string<char>, int>::iterator’ has no member named ‘first’
cout << p.first << ": " << p.second << endl;
error: ‘std::map<std::basic_string<char>, int>::iterator’ has no member named ‘second’
cout << p.first << ": " << p.second << endl;
error: ‘std::map<std::basic_string<char>, int>::iterator’ has no member named ‘first’
F << p.first << ": " << p.second << endl;
error: ‘std::map<std::basic_string<char>, int>::iterator’ has no member named ‘second’
F << p.first << ": " << p.second << endl;
Why it does not work?
Iterators are like pointers and must be dereferenced for use:
cout << p->first << ": " << p->second << endl;
The ranged-for loop (the example with auto) did this for you.
I want to overwrite an element corresponding to an key with new value.
What function I can use for this?
I tried by calling fun
myMap.add(key, value)
But this is not replacing the value already stored in map.
add() should work, here is an example:
OMUMap myMap;
char key1[] = "key-1";
char key2[] = "key-2";
/* initialize map */
myMap.add(static_cast<void*>(key1), static_cast<void*>("foo"));
myMap.add(static_cast<void*>(key2), static_cast<void*>("bar"));
std::cout << key1 << ": " << static_cast<char*>(myMap.getAt(key1)) << std::endl;
std::cout << key2 << ": " << static_cast<char*>(myMap.getAt(key2)) << std::endl;
/* replace 'key-1' value */
myMap.add(static_cast<void*>(key1), static_cast<void*>("bazzz"));
std::cout << key1 << ": " << static_cast<char*>(myMap.getAt(key1)) << std::endl;
std::cout << key2 << ": " << static_cast<char*>(myMap.getAt(key2)) << std::endl;
Output:
key-1: foo
key-2: bar
key-1: bazzz
key-2: bar
I have a map declared as follows
map<string, int> symbolTable;
if(tempLine.substr(0,1) == "("){
symbolTable.insert(pair<string, int>(tempLine, lineCount));
}
How do I std::cout all of the things in my symbol table?
In modern C++:
for (auto&& item : symbolTable)
cout << item.first << ": " << item.second << '\n';
If you only have access to a pre-C++11 compiler the code would be:
for ( map<string, int>::const_iterator it = symbolTable.begin(); it != symbolTable.end(); ++it)
cout << it->first << ": " << it->second << '\n';
Here's an alternative if your compiler isn't C++11 compliant:
for (map<string, int>::iterator it = symbolTable.begin();
it != symbolTable.end(); ++it)
{
cout << it->first << " " << it->second << endl;
}
And for completeness, if it is:
for (auto& s : symbolTable)
{
cout << s.first << " " << s.second << endl;
}
You can use a loop to print all the key/value pairs. The code following is an example in C++11
for (const auto& kv : symbolTable) {
std::cout << kv.first << " " << kv.second << '\n';
}
ps: Both of other two answers pay little attention to const, which is quite sad...
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?
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";
}
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"));
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)
Code on Coliru
You can try range based loop like this:
for(auto& x:myMap){
cout<<x.first<<" "<<x.second.first<<" "<<x.second.second<<endl;
}
The easiest way is to declare an iterator first as
map<string ,string> :: iterator it;
and after that print it out by looping over the map using iterator from myMap.begin() to myMap.end() and print out key and value pairs in the map with it->first for key and it->second for value.
map<string ,string> :: iterator it;
for(it=myMap.begin();it !=myMap.end();++it)
{
std::cout << it->first << ' ' <<it->second;
}