【发布时间】:2011-01-02 09:20:02
【问题描述】:
例如,我有以下值:
0 0 0 1 3 2
这些值是指集群 ID,其中集群的成员是 向量的索引。因此我们想要得到这样的输出:
Cluster 0 -> 0,1,2
Cluster 1 -> 3
Cluster 2 -> 5
Cluster 3 -> 4
我尝试了以下构造,但它似乎不起作用: 有什么办法呢?
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <map>
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 myfile (arg_vec[1]);
map <int, vector <int> > CCTagMap;
if (myfile.is_open())
{
// Skip First Line
getline(myfile,line);
while (getline(myfile,line) )
{
stringstream ss(line);
int CcId;
int TagId = -1;
vector <int> Temp;
while (ss >> CcId) {
TagId++;
cout << CcId << "-" << TagId << endl;
# this way to cluster doesn't seem to work
CCTagMap.insert(make_pair(CcId,Temp.push_back(TagId)));
}
}
myfile.close();
}
else { cout << "Unable to open file\n";}
return 0;
}
【问题讨论】:
标签: c++ data-structures hash map vector