c ++中的空白识别

d3v*_*pro 0 c++

我的代码必须使用cin识别空白字符,所以当我使用空格作为输入时,它应该识别空间.我该怎么做呢?

sth*_*sth 7

您可以使用它std::noskipws来禁用std::cin默认情况下的空格跳过:

#include <iostream>
#include <iomanip>

int main() {
  char c;
  std::cin >> std::noskipws;
  while (std::cin >> c) {
    if (c == ' ')
      std::cout << "A space!" << std::endl;
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)