我具有例如下列值:自動聚類隨着散列/地圖載體的在C++
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;
}
我不禁發出信號,說明'map :: insert(value_type)'返回迭代器,所以你可以在'if'塊中指定'iter',只有一次''> second.push_back (TagId)'位(並且沒有'else'子句)...但是這可能是挑剔的:P – 2010-01-07 18:53:20