writing std::map to file?

Started by
5 comments, last by Bregma 18 years, 4 months ago
Hello all. I'm using a map type from the C standard library to store contents of an options file. Specifically, my problem is writing its contents back to a file. The std::map does not seem to have a way of iteratively accessing its contents for writing back to the file. If I remember correctly, std::map sorts by key on insertions so it would make sense not to be able to access its members in this way. So my question is, how can I dump the contents of the std::map to a text file without considering specific key names? I've looked over the iterators it uses, I did not see (or may have missed) any way to get from one item to the next. I thought at first by getting an iterator to the beginning of the map would be the key to going through it all, but the things I tried did not seem to work. If it helps, I am using the implementation provided with MS Visual C++ 6.0
Advertisement
I just found this code snippet here. Below are the relevant parts..

#include <map>#include <string>#include <iostream>using namespace std;int main(){    map<char,string> mymap;    map<char,string>::iterator iter;    string s1 = "This";    string s2 = "is";    string s3 = "an";    string s4 = "example";    string s5 = "string";    mymap[s1[0]] = s1;    mymap[s2[0]] = s2;    mymap[s3[0]] = s3;    mymap[s4[0]] = s4;    mymap[s5[0]] = s5;    // Here is the output of the map in order from beginning to end    cout << "This is the output of the map in order" << endl;    for(iter = mymap.begin(); iter != mymap.end(); iter++)    {        cout << (*iter).first << " is the first character in the word " << iter->second << endl;        }}


Hope this helps.
Progress is born from the opportunity to make mistakes.

My prize winning Connect 4 AI looks one move ahead, can you beat it? @nickstadb
Why would it make sense for a map not to support iterators just because it's key - sorted?

std::map::iterator mIter works just fine.
Quote:The std::map does not seem to have a way of iteratively accessing its contents for writing back to the file.


Sure it does; it offers an iterator interface just like any other container in the Standard library:
std::map<int, std::string> map;map[2] = "foo";map[65] = "bar";map[31] = "baz";for(std::map<int, std::string>::iterator i = map.begin(); i != map.end(); ++i)  std::cout << i->first << ": " << i->second << '\n';

outputs:
2: foo31: baz65: bar
Just use copy with an ostream_iterator defined in <iterators>. It's basically an insert interator for an output stream.
Keys to success: Ability, ambition and opportunity.
Even though being a very idiomatic approach, it has a drawback: std::map<>::value_type (that is, std::pair<>) doesn't have a stream insertion operator defined! So, before it works, you have to write the overload yourself, and insert it into namespace std! Otherwise, the overload resolution (Koenig lookup) isn't going to find it (*.


*) I'm not completely sure about this - it may be that gcc has a bug or a "feature" in their implementation of ADL. Either way, even if your compiler finds the operator in global namespace, the code won't be portable to gcc, at least for the time being.
Quote:Original post by LilBudyWizer
Just use copy with an ostream_iterator defined in <iterators>. It's basically an insert interator for an output stream.


Can't do.

Use std::transform and write yourself a functor to output the pair::second value to the std::ostream_iterator (or std::ostreambuf_iterator).

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement