将vector中的三维点写入txt
时间: 2024-04-15 16:30:13 浏览: 22
您可以使用C++中的文件操作来将三维点的向量写入到txt文件中。下面是一个示例代码:
```cpp
#include <iostream>
#include <fstream>
#include <vector>
struct Point3D {
double x, y, z;
};
void writePointsToFile(const std::vector<Point3D>& points, const std::string& filename) {
std::ofstream outputFile(filename);
if (outputFile.is_open()) {
for (const auto& point : points) {
outputFile << point.x << " " << point.y << " " << point.z << std::endl;
}
outputFile.close();
std::cout << "Points have been written to " << filename << std::endl;
} else {
std::cerr << "Unable to open file: " << filename << std::endl;
}
}
int main() {
std::vector<Point3D> points = {
{1.0, 2.0, 3.0},
{4.0, 5.0, 6.0},
{7.0, 8.0, 9.0}
};
writePointsToFile(points, "points.txt");
return 0;
}
```
在上述代码中,我们定义了一个 `Point3D` 结构体来表示三维点,包含了x、y、z三个坐标。然后,我们使用 `writePointsToFile` 函数将点的坐标写入到指定的文件中。最后,我们在 `main` 函数中创建了一个包含三个点的向量,并将其写入到名为 "points.txt" 的文件中。
执行上述代码后,将会在当前目录下生成一个名为 "points.txt" 的文件,其中包含了三个点的坐标信息。