【发布时间】:2017-02-22 12:37:34
【问题描述】:
我对 C++ 非常陌生,我正在尝试在从文本文件读取的 2d 平面中创建一个点向量。为此,我首先创建一个由两个值 (x,y) 组成的结构,称为 point。然后这些点的向量称为 vec。但是,当文本文件位于三列时,我不确定如何填充结构数据!第一列只是点的索引,第二列是 x 数据,第三列是 y 数据。我不知道 vec 的大小,所以我尝试使用push_back() 这是我目前所拥有的。
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);
}
【问题讨论】:
标签: c++