2009-03-16 14 views
9

Mam następujące dane jako dane wejściowe (posortowane według pierwszej kolumnie):jak stworzyć mapę wektora Od posortowanych danych

foo 1 2 
foo 3 3 
bar 10 11 

Chcę utworzyć mapę wektora z pierwszej kolumny jako klucz mapy takie mamy:

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

Ale dlaczego mój poniższy kod nie działa zgodnie z oczekiwaniami?

#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; 
     } 
    } 

     return 0; 
    } 

Odpowiedz

16

Jak powiedział Mykola, należy użyć wektora na mapie, zamiast tworzyć go samemu. Zmieniłem twój cały kod, więc to działa dla mnie. Zauważ, że napisałeś niektóre nazwy zmiennych z niewłaściwym przypadkiem (MyMapOfVec zamiast myMapOfVec), co doprowadziło do błędów kompilatora.

Upewnij się również, że nie masz nowego wiersza na końcu pliku wejściowego, ponieważ spowoduje to powtórzenie ostatniego wiersza.

#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; 
    string KEY;  


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

      ss >> KEY >> VAL1 >> VAL2; 

      myMapOfVec[KEY].push_back(VAL1); 
      myMapOfVec[KEY].push_back(VAL2); 

     } 
     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; 
    cout << Key; 
    for (unsigned i = 0; i < tempVec.size(); i++) { 
     cout << " " << tempVec[i]; 
    } 
    cout << endl; 
} 

    return 0; 
} 

Dla przykładu, to daje wyświetlamy

bar 10 11 
foo 1 2 3 3 
1

Nie dodawaj czeku dla KEY_TEMP! = KEY. Ponieważ w twoim przypadku są równe, że foo idzie dwa razy jeden po drugim. Po prostu

myMapOfVec[KEY].push_back(VAL1); 
myMapOfVec[KEY].push_back(VAL2); 
Powiązane problemy