迭代器,用于使用 C++ 计算 CSV 中的列数
Iterator to count the number of columns in csv using C++
我正在遵循洛基·阿斯塔里的解决方案代码
如何读取和解析 C++ CSV 文件?
如何在主函数中编写迭代器来计算CSV标头中的列数
int main()
{
std::ifstream file("plop.csv");
for(CSVIterator loop(file); loop != CSVIterator(); ++loop)
{
//Instead of printing the 4th element as shown below, I want to print all the
//columns and thus determine the number of columns
//std::cout << "4th Element(" << (*loop)[3] << ")n";
}
}
这是我正在使用的 csv 文件的示例标头
cmd, id, addr, qos, len, lock, prot, burst, size, cache, user, duser, dstrb, data
我想使用迭代器或一些 for 循环打印它们并确定列数,在本例中为 14
如果你通读CSVIterator
代码,它使用具有以下方法的CSVRow
类:
std::size_t size() const
{
return m_data.size();
}
其中m_data
是一个std::vector<std::string>
,其中每个std::string
是行中的单个列。因此,调用 CSVRow::size
返回列数。
int main()
{
std::ifstream file("plop.csv");
for(CSVIterator loop(file); loop != CSVIterator(); ++loop)
{
const auto numCols = (*loop).size();
std::cout << "Number of Columns: " << numCols << std::endl;
for(std::size_t i = 0; i < numCols; ++i)
{
// Print each column on a new line
std::cout << (*loop)[i] << std::endl;
}
}
}
对于输入:
cmd, id, addr, qos, len, lock, prot, burst, size, cache, user, duser, dstrb, data
输出:
Number of Columns: 14
cmd
id
addr
qos
len
lock
prot
burst
size
cache
user
duser
dstrb
data
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 递归函数计算序列中的平方和(并输出过程)
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的字符计数代码计算错误.为什么
- 在计算中使用二的幂有多有利可图
- 正在将csv文件读取为双精度矢量
- 如何计算文件中的"columns"数?
- 计算排序向量的向量中唯一值的计数
- 如何使用 std::累积在 C++ 中计算总和立方体
- 使用Qt C++计算类似Git的SHA1哈希
- OpenCV C++.快速计算混淆矩阵
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- C++如何计算用户输入的数字中的偶数位数
- 如何计算数据类型的范围,例如int
- 类似枚举的计算常量
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 快速计算 CSV 文件 C++ 中的行数
- 迭代器,用于使用 C++ 计算 CSV 中的列数
- CSV文件中的两条线的交点的计算返回-1.#IND