-2

The following is my code, I am reading data in from a file, and taking the data one line at a time and trying to insert it into a map. I am not allowed to use more then one map.

using namespace std;

int main( int argc, char* argv[]){

    char* file = argv[1];          // saves name of file
    ifstream infile ( file );      // Imported file
    map <string,  set < pair < string, string  > > > m;
    string data;
    string key;
    string year;
    string count;

    if(!infile){
        cout << "Error opening file" << endl;
        return -1;
    }

    while (!infile.eof() ){
        getline(infile, data);

        for ( unsigned int i = 0; i < data.length(); i++ ){
            while ( data[i] != '\t'){
                key += data[i];
                i++;
            }
            i++;
            while ( data[i] != '\t'){
                year += data[i];
                i++;
            }
            i++;
            while ( data[i] != '\t'){
                count += data[i];
                i++;
            }
            cout << key << endl;
            cout << year << endl;
            cout << count << endl;
            cout << endl;
            break;
        }

        m.insert(key, set < pair < string, string > > ( year, count)  );
        key.clear();
        year.clear();
        count.clear();
    }

}
knole
  • 1
  • Could you please ask a real question ? Compiler error would be fine too, if there aren't any, tell us what it is doing and what you expect. Explain us a little bit more what you are doing. – Telokis Mar 06 '15 at 01:36
  • `while (!infile.eof() )` this is wrong. It will break for an empty file. It should be `while (getline(infile, data)) {}` – Neil Kirk Mar 06 '15 at 01:46
  • You should change your `while` loop to `while (getline(infile, data))`. Read this article: [Why EOF in while loop is bad.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Thomas Matthews Mar 06 '15 at 01:47
  • You don't check for buffer overrun in your `while (data[i]` loops. – Thomas Matthews Mar 06 '15 at 01:49
  • Try using `std::string::find_first_not_of("\t")` and `std::string::substr()`. – Thomas Matthews Mar 06 '15 at 01:51

2 Answers2

0

You define here a map with a key and a value which is a set of pairs:

map <string,  set < pair < string, string  > > > m;

Is this really what you want ?

If you want to associate a pair of strings to a key, then define your map simply as:

map <string,  pair < string, string  >  > m;

To insert a new value you could then do:

m[key] = make_pair (year, count); 

If on the other side, you'd prefer to associate several distinct pairs to a same key, then you could use a multimap

P.S.: It's not directly related to your question, but never llop on eof in C++. Use while (getline(infile, data) ) instead.

Christophe
  • 68,716
  • 7
  • 72
  • 138
0

The problem is that you are inserting in the map, but you want to insert in the set that is at the key key in the map

Try changing

 m.insert(key, set < pair < string, string > > ( year, count)  );

With:

m[key].insert(make_pair(year, count));

Or (more robust):

map <string,  set < pair < string, string  > > >::iterator it = m.find(key)
if (key == m.end) {
    set <pair<string,string>> s;
    s.insert(make_pair(year, count));
    m.insert(make_pair(key, s));
} else {
    it->second.insert(make_pair(year, count));
}
ichramm
  • 6,437
  • 19
  • 30