C++ 如何从排序数据创建矢量地图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/649793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Howto Create Map of Vector From Sorted Data
提问by neversaint
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;
}
}
return 0;
}
回答by schnaader
As Mykola said, you should use the vector in the map instead of creating one yourself. I changed your whole code so it works for me. Note that you wrote some of the variable names with wrong case (MyMapOfVec instead of myMapOfVec) and this led to compiler errors.
正如 Mykola 所说,您应该使用地图中的矢量,而不是自己创建矢量。我改变了你的整个代码,所以它对我有用。请注意,您使用错误的大小写(MyMapOfVec 而不是 myMapOfVec)编写了一些变量名称,这导致了编译器错误。
Also be sure you don't have a newline at the end of your input file because this will result in repeating the last line.
还要确保输入文件末尾没有换行符,因为这会导致重复最后一行。
#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;
}
For your example, this gives the output
对于您的示例,这给出了输出
bar 10 11
foo 1 2 3 3
回答by Mykola Golubyev
Don't add check for KEY_TEMP != KEY. Because in your case they are equal as foo goes two times one by one. Just
不要为 KEY_TEMP != KEY 添加检查。因为在您的情况下,它们相等,因为 foo 一次接两次。只是
myMapOfVec[KEY].push_back( VAL1 );
myMapOfVec[KEY].push_back( VAL2 );
回答by blue01
Some basics about maps and vectors - http://rowsandcolumns.blogspot.com/2010/10/c-maps-and-vectors.html
关于地图和矢量的一些基础知识 - http://rowsandcolumns.blogspot.com/2010/10/c-maps-and-vectors.html