C :构成CSV文件列的向量
C++ : Vectors forming the columns of a CSV file?
是否有一种方法可以在C 中使用一系列向量并输出一个CSV文件,其中CSV文件的列分别是向量的元素?因此,第1列将是示例的第一个双矢量的元素。
#include <vector>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
// series of vectors could be put in another collection
vector<double> col1 = { 0, 1, 2 };
vector<double> col2 = { 3, 4, 5 };
vector<double> col3 = { 6, 7, 8 };
ofstream csvfile("series_vectors.csv");
if (csvfile.is_open())
{
// it is possible that the vectors are not of the same size
// if you get the maximum, then you may need to fill in empty fields
// for some of the columns
int num_of_rows = min({ col1.size(), col2.size(), col3.size() });
for (int i = 0; i < num_of_rows; i++)
{
csvfile << col1[i] << "," << col2[i] << "," << col3[i] << endl;
}
}
else
{
cout << "File could not be opened";
}
return 0;
}
相关文章:
- 如何从文件中读取两个字符串和数字数组,并将它们存储在对象向量中
- 如何在目录及其子文件夹中构建文件名字符串向量?
- 尝试将字符串从文件读取到无符号字符向量中
- 如何将包含另一个对象向量的对象保存到文件中,并使用C++中的二进制文件从文件中读回?
- 将结构向量保存到文件中,并从C++文件中读取结构向量
- C++,从文件读取到结构,然后读取到向量(结构被推入向量太多次,而不仅仅是一次)
- 从.txt文件读取到C++中的双精度向量
- 遍历对象向量,并找到与从文本文件中提取的对象匹配的变量
- C++ 将 CSV 文件的列写入向量
- 数组向量的文件 I/O
- 如何在C++中将.csv文件的元素存储到二维向量中?
- 包含换行符分隔的单词的文件和C++中这些单词的字符串向量的大小是否相同?
- C++文件中的向量
- 如何从输入文件中读取字符并将其存储到向量中?
- 如何从文件中为C++中的向量赋值?
- 将无符号字符的向量写入二进制文件 c++
- 通过将文本文件读取为字符串/向量来计算加权/未加权 GPA
- 如何将目录中的文件属性填充到结构向量中?
- 如何从文件中读取 n 个字节并使用<uint8_t>迭代器将它们放入向量中?
- 类对象的向量C++文件返回程序