質問

入力として次のデータがあります(最初の列でソートされています):

foo 1 2
foo 3 3
bar 10 11

マップのキーとして最初の列を持つベクターのマップを作成したい 次のようになります:

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

しかし、以下のコードが期待どおりに機能しないのはなぜですか?

    #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;
    }
役に立ちましたか?

解決

Mykolaが言ったように、マップを自分で作成するのではなく、マップで使用する必要があります。あなたのコード全体を変更したので、それは私のために機能します。間違ったケース(myMapOfVecではなくMyMapOfVec)を使用して変数名の一部を記述したため、コンパイラエラーが発生したことに注意してください。

また、入力ファイルの最後に改行がないことを確認してください。これにより、最後の行が繰り返されることになります。

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

例では、これにより出力が得られます

bar 10 11
foo 1 2 3 3

他のヒント

KEY_TEMP!= KEYのチェックを追加しないでください。あなたの場合、fooは2回ずつ1つずつ進むので等しいからです。ただ

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

マップとベクターに関する基本事項- http://rowsandcolumns.blogspot.com/2010/10/c -maps-and-vectors.html

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top