【C++】 使用vector存储三维坐标文件
【思路】:首先把文件(.txt)里的数据以每一个数为单位导入到vector容器里面,然后创建包含结构体的vector,按顺序以每三个为一组(代表x、y、z)存储到结构体容器中。【数据和结果】:【测试代码】:#include <iostream>#include <fstream>#include <string>#inclu...
·
【思路】:
首先把文件(.txt)里的数据以每一个数为单位导入到vector容器里面,然后创建包含结构体的vector,按顺序以每三个为一组(代表x、y、z)存储到结构体容器中。
【数据和结果】:
【测试代码】:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <algorithm>
#include <vector>
struct MyPoint //创建<点>结构体
{
float x;
float y;
float z;
};
void showMyPoint(const MyPoint &mm);
using namespace std;
int main()
{
vector <float> everypoints;//每个点为一个单位存储的数组
vector <MyPoint> Mypoints; //创建点的数组
MyPoint mypoint; //实例化点
float ch;
ifstream fin;
fin.clear();
fin.open("C:\\Users\\Administrator\\Desktop\\点.txt");
for (int i = 0; !fin.eof(); i++)
{
fin >> ch;
everypoints.push_back(ch);
fin.get();//分隔符,
}
for (int ii = 0; ii<5; ii++) //5行
{
float x_one = everypoints[3 * ii];
float y_two = everypoints[3 * ii + 1];
float z_three = everypoints[3 * ii + 2];
mypoint.x = x_one;
mypoint.y = y_two;
mypoint.z = z_three;
Mypoints.push_back(mypoint);
}
cout << " x: " << " y: " << " z: " << endl;
for_each(Mypoints.begin(), Mypoints.end(), showMyPoint); cout << endl;
fin.close();
return 0;
}
void showMyPoint(const MyPoint & mm)
{
cout <<" "<< mm.x << " | " << mm.y << " | " << mm.z << endl;
}
All that really matters is you don't stop trying.
最关键的是,不要放弃尝试。
更多推荐
已为社区贡献1条内容
所有评论(0)