2009-03-16 40 views
9

Tôi có dữ liệu sau đây như là đầu vào (được sắp xếp theo cột đầu tiên):Howto Tạo Map của Vector Từ Sắp xếp dữ liệu

foo 1 2 
foo 3 3 
bar 10 11 

Tôi muốn tạo ra một bản đồ của Vector với cột đầu tiên làm chủ chốt của bản đồ như vậy mà chúng tôi có:

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

Nhưng tại sao mã của tôi bên dưới không hoạt động như mong đợi?

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

Trả lời

16

Như Mykola nói, bạn nên sử dụng các vector trong bản đồ thay vì tạo một mình. Tôi đã thay đổi toàn bộ mã của bạn để nó hoạt động cho tôi. Lưu ý rằng bạn đã viết một số tên biến với trường hợp sai (MyMapOfVec thay vì myMapOfVec) và điều này dẫn đến lỗi trình biên dịch.

Cũng đảm bảo bạn không có dòng mới ở cuối tệp nhập vì điều này sẽ dẫn đến lặp lại dòng cuối cùng.

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

Ví dụ của bạn, điều này sẽ cho sản lượng

bar 10 11 
foo 1 2 3 3 
1

Không thêm séc cho KEY_TEMP! = KEY. Bởi vì trong trường hợp của bạn, họ bằng nhau như foo đi hai lần một. Chỉ cần

myMapOfVec[KEY].push_back(VAL1); 
myMapOfVec[KEY].push_back(VAL2); 
Các vấn đề liên quan