Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 计算以空格分隔的文件的列数_C++_Count_Stdin - Fatal编程技术网

C++ 计算以空格分隔的文件的列数

C++ 计算以空格分隔的文件的列数,c++,count,stdin,C++,Count,Stdin,我的问题涉及如何使用std::count(或其他适当的函数)来计算以空格分隔的文件的列数 我现在用的是这样的东西: std::ifstream inFile("file"); int lines = std::count(std::istreambuf_iterator<char>(inFile), std::istreambuf_iterator<char>(), '\n'); std::ifstream infle(“文件”);

我的问题涉及如何使用
std::count
(或其他适当的函数)来计算以空格分隔的文件的列数

我现在用的是这样的东西:

  std::ifstream inFile("file"); 
  int lines = std::count(std::istreambuf_iterator<char>(inFile), 
             std::istreambuf_iterator<char>(), '\n');
std::ifstream infle(“文件”);
int line=std::count(std::istreambuf_迭代器(infle),
std::istreambuf_迭代器(),“\n”);
数一数

因为所有的行都相等(数据量相同),所以

  std::ifstream inFile("file"); 
  int columns = std::count(std::istreambuf_iterator<char>(inFile), 
             std::istreambuf_iterator<char>('\n'), ' ') + 1;
std::ifstream infle(“文件”);
int columns=std::count(std::istreambuf_迭代器(infle),
std::istreambuf_迭代器('\n'),'+-1;
做我需要的吗

谢谢

编辑:


我的意思是,如果在
“文件”
中有像
12
1[此处有许多空格]2
这样的数据,
列的值是否为2?

不,您将计算空格,而不是列。您需要标记行,例如,在简单情况下,
列数=空格数+1
。我明白了。如果我不允许(不问为什么)使用外部库,那怎么办?你可以使用C样式,我没有推荐它,因为你指定了[C++ ],因为,我用C++,我想尽可能多地使用它。谢谢你的邀请reply@Ed:因为我只有一台机器,目前正忙于繁重的计算,同时我正在考虑如何解决这个问题。