我想知道如何動態地將數據插入到集合中。 我有一個不同點的文本文件,我需要動態插入到集合中,因爲我不知道有多少項目會在那裏。動態插入到集合
sample.txt的
Point [3 4]
Point [5 6]
的main.cpp
set<Point> s_p2;
if (strData.find("Point") != string::npos) {
pos = strData.find("t");
strData = strData.substr(pos + 2, 4);
istringstream in(strData);
Point temp_p;
in >> temp_p;
s_p2.insert(temp_p);
}
s_p2
是該組容器和下面的代碼集被環直到文件的末尾。 Q1:如果我這樣做,我的套裝只有1件或多件temp_p
?第二季度:我怎樣才能打印出側面的價值?
。
ostream& operator<<(ostream &out, Point &p2) {
p2.setDistFrOrigin();
out << "[" << setw(4) << p2.getX() << setw(1) << "," << setw(4) << p2.getY() << "] " << setprecision(3) << p2.getScalarValue() << endl;
}
您的編輯完全改變了問題,因此發佈的答案沒有意義。我已經回到原來的問題;如果您有更多問題,請分別提問。要回答你的新問題:你需要聲明成員函數'const',比如'類型getX()const',以便在聲明爲'const'的對象上調用它們。 –