Alex Rivera | Logout

Howto Create Map of Vector From Sorted Data

Asked 2009-03-16T09:54:43.950
9

I have the following data as input (sorted by first column):

foo 1 2
foo 3 3
bar 10 11

I want to create a Map of Vector with first column as key of the map such that we have:

foo = {1,2,3,3}
bar = {10,11}

But why my code below doesn't work as expected?

    #include <vector>
    #include <map>
    #include <iostream>
    #include <fstream>
    #include <sstream>

    using namespace std;

    int main  ( int arg_count, char *arg_vec[] ) {
        if (arg_count !=2 ) {
            cerr << "expected one argument" << endl;
            return EXIT_FAILURE;      
        }

        string line;         
        ifstream acemblyfile (arg_vec[1]); 

        map <string, vector<int> > myMapOfVec; 
        vector <string> myVec;  
        string KEY = "" ;    


        if (acemblyfile.is_open())
        {
            while (getline(acemblyfile,line) )
            {
                stringstream ss(line);    
                string KEY_TEMP;
                int VAL1;
                int VAL2;           

                ss >> KEY_TEMP >> VAL1 >> VAL2;

                MyVec.push_back(VAL1);   
                MyVec.push_back(VAL2);   


                if (KEY_TEMP != KEY) {     
                  myMapOfVec[KEY] = MyVec;
                  KEY = KEY_TEMP;            
                  MyVec.clear();          
                }

            }
            acemblyfile.close();      
        }
        else {
            cout << "Unable to open file"; 
        }


for( map<string, vector<int> >::iterator iter = myMapOfVec.begin(); iter != myMapOfVec.end(); ++iter ) {
       vector <int> tempVec = (*iter).second;
       string Key = (*iter).first;
       for (unsigned i =0; i<tempVec.size(); i++) {
          cout << Key << " " << tempVec[i] << endl;
       }
    }

     
Edit
Report

1 Answer

1

Don't add check for KEY_TEMP != KEY. Because in your case they are equal as foo goes two times one by one. Just

myMapOfVec[KEY].push_back( VAL1 );
myMapOfVec[KEY].push_back( VAL2 );
answered 2009-03-16T10:02:50.140

Your Answer