我想让它把任何不是字母字符的东西都当作分隔符。我该怎么做?
最佳答案
你不能。默认分隔符是 \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_of
或 find_last_not_of
.
在你的另一个问题中,你确定你已经考虑了所有的答案吗?一个使用istream::operator>>(std::istream&, <string>)
, 它将匹配一系列非空白字符。
关于c++ - 你能在 std::getline 中指定什么不是定界符吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9272276/