c++ - 你能在 std::getline 中指定什么不是定界符吗?

标签 c++ getline stdstring

我想让它把任何不是字母字符的东西都当作分隔符。我该怎么做?

最佳答案

你不能。默认分隔符是 \n :

while (std::getline (std::cin, str) // '\n' is implicit

对于其他分隔符,传递它们:

while (std::getline (std::cin, str, ' ') // splits at a single whitespace

但是,分隔符是char类型的,所以只能使用一个“分割字符”,不能匹配什么不匹配。

如果您的输入恰好位于容器内,如 std::string , 你可以使用 find_first_not_offind_last_not_of .


在你的另一个问题中,你确定你已经考虑了所有的答案吗?一个使用istream::operator>>(std::istream&, <string>) , 它将匹配一系列非空白字符。

关于c++ - 你能在 std::getline 中指定什么不是定界符吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9272276/

相关文章:

c++ - For 循环永远持续下去

c++ - 在声明后将 decltype 与成员函数定义一起使用

c++ - 标准库 to_string(double) 在 vs2015 中给出了错误的值。有什么解决办法吗?

c++ - C++ 中的 Getline 函数将第一个输入作为空字符。它应该这样做吗?

c++ - std::string::replace() 是否针对相同长度的字符串进行了优化?

c++ - 在结构 vector 中,我应该在结构中使用 char[] 而不是 std::string 以便它们是 POD 吗?

c++ - 如何确定进程是 32 位还是 64 位?

c++ - 输入float后用户输入结束

c++ - 如何正确使用getline

c++ - 将内存中的16位转换为std::string