I have a map <string, vector of ints> and would like to display what I hope I have loaded into the map...however, it appears that I have to define a << operator that takes a type HEADINGMAP (my map type).
Could anyone pls assist.
Thanks
s
I have a map <string, vector of ints> and would like to display what I hope I have loaded into the map...however, it appears that I have to define a << operator that takes a type HEADINGMAP (my map type).
Could anyone pls assist.
Thanks
s
This is the best I can do with what little detail you've provided.
Code:ostream& operator<<(ostream& out, const HEADINGMAP& hmap) { // Code to write the contents of hmap to out return out; }
When writing a specialization, be careful about its location; or to make it compile will be such a trial as to kindle its self-immolation.
Thanks Kip,
I have included your code - but am now getting compile errors...
error C2143: syntax error : missing ',' before '&'
does hmap have to be declared elsewhere - in a .h ?? It says it is undeclared...
I have tried to use:
out << *hmap->first << ": ";
to display the string portion of the map, but am getting a compile error - error C2227: left of '->first' must point to class/struct/union
I need to be able to access the key and values within the map - in order to write them to a report.
Thanks for your help
sue
This has all the signs of HEADINGMAP not being a type. What does the declaration of HEADINGMAP look like? I was under the impression that you were doing something like this.Originally Posted by Sue Paterniti
Code:typedef map< string, vector<int> > HEADINGMAP; [...] ostream& operator<<(ostream& out, const HEADINGMAP& hmap) { HEADINGMAP::const_iterator i = hmap.begin(); for (; i != hmap.end(); ++i) { out << i->first << ": "; } return out; }
When writing a specialization, be careful about its location; or to make it compile will be such a trial as to kindle its self-immolation.
Sue,
You need to read up on operator overloading. Then you'll be able to make sense of Kips code and implement it into your code.
Its not a large topic for what you need, and should help you.
hth
Open source isn't a matter of life or death......
.......its much more important than that!!
SuSE Linux - GCC 3.4.2
XP Pro - Visual Studio 2005 TS, MinGW 3.4.2
Hi Kip,
I have typedef'd an int vector as typedef vector<int> INTVEC;
Then typdef'd the map as typedef map <string, INTVEC > HEADINGMAP;
and an iterator as typedef HEADINGMAP::iterator HEADMAPIT;
am still getting the compile error that there is a missing ',' before a '&' - but will do some more reading as recommended...
thanks
s