Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.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+;计算文本文件中一行左侧的空白+;_C++_Count_Whitespace - Fatal编程技术网

C++ 使用C+;计算文本文件中一行左侧的空白+;

C++ 使用C+;计算文本文件中一行左侧的空白+;,c++,count,whitespace,C++,Count,Whitespace,我只是想计算文本文件中一行左边的空格数。 我一直在使用 count( line.begin(), line.end(), ' ' ); 但很明显,这包括所有左边的空格,单词之间的空格和右边的空格。 所以基本上我想要它做的是,一旦它击中一个非空格字符,停止它计算空格 谢谢大家。怎么样 line.find_first_not_of(' '); 编辑: 如果是所有空格: unsigned int n = line.find_first_not_of(' '); if(n==s.npos)

我只是想计算文本文件中一行左边的空格数。 我一直在使用

count( line.begin(), line.end(), ' ' );
但很明显,这包括所有左边的空格,单词之间的空格和右边的空格。 所以基本上我想要它做的是,一旦它击中一个非空格字符,停止它计算空格

谢谢大家。

怎么样

line.find_first_not_of(' ');
编辑: 如果是所有空格:

unsigned int n = line.find_first_not_of(' ');
if(n==s.npos)
    n = line.length();
怎么样

line.find_first_not_of(' ');
编辑: 如果是所有空格:

unsigned int n = line.find_first_not_of(' ');
if(n==s.npos)
    n = line.length();
线是一根线吗

在这种情况下,您需要
std::string::find_first_not_of
来查找第一个非空白,然后对行的其余部分使用
std::count
,如下所示:

std::string::size_type firstNonSpace = line.find_first_not_of(' ');
std::size_t result = std::count(line.begin()+(firstNonSpace==std::string::npos?0:firstNonSpace),line.end(),' ');
线是一根线吗

在这种情况下,您需要
std::string::find_first_not_of
来查找第一个非空白,然后对行的其余部分使用
std::count
,如下所示:

std::string::size_type firstNonSpace = line.find_first_not_of(' ');
std::size_t result = std::count(line.begin()+(firstNonSpace==std::string::npos?0:firstNonSpace),line.end(),' ');

查找第一个非空白字符

std::string            test = "     plop";
std::string::size_type find = test.find_first_not_of(" \t");  // Note: std::string::npos returned when all space.
从技术上讲不是空白(因为其他字符也是空白)。
您是否正在尝试计算或删除空白

如果您试图去除空白,那么流操作符会自动执行

std::stringstream testStream(test);
std::string       word;

testStream >> word;  // white space stripped and first word loaded into 'word'

查找第一个非空白字符

std::string            test = "     plop";
std::string::size_type find = test.find_first_not_of(" \t");  // Note: std::string::npos returned when all space.
从技术上讲不是空白(因为其他字符也是空白)。
您是否正在尝试计算或删除空白

如果您试图去除空白,那么流操作符会自动执行

std::stringstream testStream(test);
std::string       word;

testStream >> word;  // white space stripped and first word loaded into 'word'

假设
line
std::string
,那么:

#include <algorithm>
#include <cctype>
#include <functional>

std::string::const_iterator firstNonSpace = std::find_if(line.begin(), line.end(),
   std::not1(std::ptr_fun<int,int>(isspace)));
int count = std::distance(line.begin(), firstNonSpace);
#包括
#包括
#包括
std::string::const_迭代器firstNonSpace=std::find_if(line.begin(),line.end(),
std::not1(std::ptr_fun(isspace));
int count=std::distance(line.begin(),firstNonSpace);

假设
标准::字符串
,那么:

#include <algorithm>
#include <cctype>
#include <functional>

std::string::const_iterator firstNonSpace = std::find_if(line.begin(), line.end(),
   std::not1(std::ptr_fun<int,int>(isspace)));
int count = std::distance(line.begin(), firstNonSpace);
#包括
#包括
#包括
std::string::const_迭代器firstNonSpace=std::find_if(line.begin(),line.end(),
std::not1(std::ptr_fun(isspace));
int count=std::distance(line.begin(),firstNonSpace);


什么是
?A
std::string
?您是在寻找非空格还是非空格?@mmyers:我认为这是一个合理的假设。。。。尽管Nick Meyer的解决方案在类型上没有成员函数的情况下也能工作(所以它也适用于vector)。对此我很抱歉,我应该包括这一点。这是一个std::字符串,是的。什么是
?A
std::string
?您是在寻找非空格还是非空格?@mmyers:我认为这是一个合理的假设。。。。尽管Nick Meyer的解决方案在类型上没有成员函数的情况下也能工作(所以它也适用于vector)。对此我很抱歉,我应该包括这一点。这是一个std::字符串,是的。+1:从技术上讲,这是唯一正确的答案,因为它可以检测真正的空白。@Martin York:是的。它返回字符串的长度
find_if
在失败时返回结束迭代器。轻微的吹毛求疵:在这里使用
std::string::const_迭代器更为一般。计算空白不会修改字符串,并且在给定常量字符串的情况下应该是可行的+反正是1。@sbi:这就是为什么我们真的需要
auto
关键字使其看起来像是C++0x:)
std::not1(isspace)
无法编译。对于
变体,它应该是
not1(ptr\u-fun(isspace))
,或者对于
的变体,它应该是
not1(bind2nd(ptr\u-fun(isspace),locale(“”))
。+1:从技术上讲,这是唯一正确的答案,因为它可以检测真正的空白。@Martin York:是的。它返回字符串的长度
find_if
在失败时返回结束迭代器。轻微的吹毛求疵:在这里使用
std::string::const_迭代器更为一般。计算空白不会修改字符串,并且在给定常量字符串的情况下应该是可行的+反正是1。@sbi:这就是为什么我们真的需要
auto
关键字使其看起来像是C++0x:)
std::not1(isspace)
无法编译。对于
变体,它应该是
not1(ptr\u-fun(isspace))
,或者对于
的变体,它应该是
not1(bind2nd(ptr\u-fun(isspace),locale(“”))
。您应该使用line.npos而不是-1。根据您使用的平台,该常数可能会有所不同。您应该使用line.npos而不是-1。根据您使用的平台,该常数可能会有所不同。当您找到第一个非空间时,您还不知道空间的大小吗?为什么要调用
std::count
?当您找到第一个非空间时,您还不知道空间量吗?为什么要调用
std::count
?只是尝试计数,而不是剥离。谢谢你,只是想数数,而不是脱衣。谢谢