正如Mykola所说,你应该在地图中使用矢量而不是自己创建矢量.我改变了你的整个代码,这对我有用.请注意,您编写了一些带有错误大小写的变量名称(MyMapOfVec而不是myMapOfVec),这导致了编译器错误.
另外请确保输入文件末尾没有换行符,因为这会导致重复最后一行.
#include #include #include #include #include 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 > 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 >::iterator iter = myMapOfVec.begin(); iter != myMapOfVec.end(); ++iter ) { vector 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