用c++将vector写入txt文件
时间: 2023-10-23 20:24:14 浏览: 104
以下是将一个整数向量写入文本文件的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <vector>
int main() {
// 创建整数向量
std::vector<int> v {1, 2, 3, 4, 5};
// 打开文件
FILE* fp = fopen("output.txt", "w");
if (fp == NULL) {
printf("无法打开文件\n");
exit(EXIT_FAILURE);
}
// 将向量中的每个元素写入文件
for (int i = 0; i < v.size(); i++) {
fprintf(fp, "%d ", v[i]);
}
// 关闭文件
fclose(fp);
return 0;
}
```
在上面的代码中,我们首先创建了一个整数向量,然后打开了一个名为`output.txt`的文本文件。接下来,我们使用`fprintf`将向量中的每个元素写入文件。最后,我们关闭了文件并退出程序。
相关问题
C++ 将成绩排序并将结果写入txt文件中
好的,您可以使用以下代码来实现:
```c++
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
struct Student {
string name;
int score;
};
bool compare(Student a, Student b) {
return a.score > b.score;
}
int main() {
vector<Student> students;
ifstream infile("input.txt");
if (!infile) {
cout << "Error opening file!" << endl;
return 0;
}
string name;
int score;
while (infile >> name >> score) {
Student student = {name, score};
students.push_back(student);
}
infile.close();
sort(students.begin(), students.end(), compare);
ofstream outfile("output.txt");
if (!outfile) {
cout << "Error opening file!" << endl;
return 0;
}
for (int i = 0; i < students.size(); i++) {
outfile << students[i].name << " " << students[i].score << endl;
}
outfile.close();
return 0;
}
```
这个程序会从名为 `input.txt` 的文件中读取学生的姓名和成绩,将它们存储到 `vector` 中。然后使用 `sort` 函数将学生按成绩从高到低排序,最后将排好序的学生写入到名为 `output.txt` 的文件中。
注意,您需要在程序运行前创建 `input.txt` 文件,并在其中按照以下格式输入学生的姓名和成绩:
```
张三 90
李四 85
王五 92
```
输出的 `output.txt` 文件将按照以下格式进行输出:
```
王五 92
张三 90
李四 85
```
希望对您有所帮助!
将vector<vector<double>> 写入txt
可以使用C++标准库中的fstream库来实现将vector<vector<double>>写入txt文件中。
以下是一个简单的例子:
```cpp
#include <iostream>
#include <fstream>
#include <vector>
int main() {
std::vector<std::vector<double>> data = {{1.2, 3.4, 5.6}, {7.8, 9.0, 1.2}};
// 打开文件
std::ofstream file("data.txt");
// 写入数据
for (const auto& row : data) {
for (const auto& element : row) {
file << element << " ";
}
file << std::endl;
}
// 关闭文件
file.close();
return 0;
}
```
在这个例子中,我们首先定义了一个vector<vector<double>>类型的变量data,然后将其写入到文件data.txt中。我们使用了ofstream类来打开文件,并使用嵌套的for循环将数据写入文件中。每行数据用换行符分隔开来。
最后,我们关闭了文件。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)