At the moment, I have simplified the problem a bit by leaving the comment logic (which in any case looks broken).
#include <map> #include <fstream> #include <iostream> #include <string> typedef std::pair<std::string, std::string> entry; // This isn't officially allowed (it an overload, not a specialization) but is // fine with every compiler of which I'm aware. namespace std { std::istream &operator>>(std::istream &is, entry &d) { std::getline(is, d.first, '='); std::getline(is, d.second); return is; } } int main() { // open an input file. std::ifstream in("myfile.ini"); // read the file into our map: std::map<std::string, std::string> dict((std::istream_iterator<entry>(in)), std::istream_iterator<entry>()); // Show what we read: for (entry const &e : dict) std::cout << "Key: " << e.first << "\tvalue: " << e.second << "\n"; }
Personally, I think that I will write the omission of the comment as a filter of the filtering stream, but for those who are not familiar with the standard C ++ library, it is open for an argument, which will be a somewhat roundabout solution. Another option would be comment_iterator , which skips the rest of the line, starting with the designated comment delimiter. I donβt like it either, but itβs probably easier in some respects.
Please note that the only code that we really write here is to read one, one record from the file in pair . istream_iterator handles almost everything. Thus, it makes little sense to write a direct analog of your function - we just initialize the map from iterators, and we are done.
Jerry Coffin Apr 21 '13 at 19:57 2013-04-21 19:57
source share