c++ - 在 std::getline() 上使用任何字符作为分隔符

标签 c++ stdout stdin getline

我正在尝试计算存储在字符串中的文件中一行开头的空格 ' ' 。 问题是我不知道如何告诉 std::getline() 在找到任何不同于 ' ' 的字符时停止。

std::getline(file_input, string_target, 'Any_character_except_space');

最佳答案

您不能使用除空格以外的任何字符 作为std::getline() 的分隔符, 没有一个签名允许这样做。

例如,您可以做的是:

std::string line;
std::getline(file_input,line);
auto pos = std::find_if_not(std::begin(line),std::end(line),[](char c) {
        return std::isspace(c);
     // or c == ' '
     // or whatever condition you need
    } );
size_t space_count = std::distance(std::begin(line),pos);

这是一个 full example .

关于c++ - 在 std::getline() 上使用任何字符作为分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45665482/

相关文章:

ruby - 如何立即打印标准输出?

unix - ksh:如何探测标准输入?

c++ - 对象的地址空间独立表示

python - Process.StandardOutput.Readline() 卡在 Mono 而不是 .NET

c# - System.Diagnostics.Debug 类是否有用于 Console.SetOut 的 TextWriter 接口(interface)?

python - 为什么此代码在 Python3.1 中的行为与在 Python2.6 中的行为不同?

c++ - 在 C++ 资源中没有更频繁地提及堆栈和堆是否有特殊原因?

c++ - Rcpp-使用优化功能

c++ - iostreams中如何处理浮点溢出

cygwin - 从 cygwin shell 启动时写入标准输出的位置在哪里,没有重定向