我很新的C++,我試圖從一個文本閱讀的2D平面創建點的矢量。爲此,我首先創建一個由兩個值(x,y)組成的稱爲point的結構。然後這些點的矢量稱爲vec。但是我不確定如何在文本文件中三列填充的結構數據!第一列是隻爲點的索引,第二列是x數據和第三是y數據。我不知道的VEC的大小,所以我嘗試使用push_back()
這是我到目前爲止所。如何填充從文本文件中矢量有兩列C++
int main(){
struct point{
std::vector<x> vec_x;
std::vector<y> vec_y;
};
std::vector<point> vec;
vec.reserve(1000);
push_back();
ifstream file;
file.open ("textfile.txt");
if (textfile.is_open()){
/* want to populate x with second column and y with third column */
}
else std::cout << "Unable to open file";
}
這裏的評論是我有以下;
while(file >> x)
vec.push_back (x);
while(file >> y)
vec.push_back (y);
道歉,如果這很簡單,但它不是我!下面發佈是僅有6分的txt文件的示例。
0 131 842
1 4033 90
2 886 9013490
3 988534 8695
4 2125 10
5 4084 474
6 863 25
編輯
while (file >> z >> x >> y){
struct point{
int x;
int y;
};
std::vector<point> vec;
vec.push_back (x);
vec.push_back (y);
}
謝謝您的幫助!然而,我仍然在努力。我編輯了這個問題,你能告訴我我是否在正確的軌道上嗎?乾杯! – AngusTheMan
@AngusTheMan有點是,但也很多沒有。請參閱我更新的答案,瞭解簡單的示例程序。 –
非常感謝! – AngusTheMan