C++ 检测空白字符、\t、\n 等

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27610900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:49:45  来源:igfitidea点击:

detecting white space characters, \t, \n, etc

c++

提问by Matt

I am working through c++ primer 5th edition. the exercise in chapter 5 states the follow:

我正在学习 C++ 入门第 5 版。第 5 章的练习说明如下:

Exercise 5.11: Write a program that counts the number of blank spaces, tabs, and newlines read.

练习 5.11:编写一个程序,计算读取的空格、制表符和换行符的数量。

I attempted to do as exercise 5.11 did with the below code. However the code fails. Can anyone provide some examples of how i would correct the code so that when the input is read that the counter unsigned int cnt = 0;will be iterated correctly?

我试图用下面的代码做练习 5.11。但是代码失败了。谁能提供一些我将如何更正代码的示例,以便在读取输入时unsigned int cnt = 0;正确迭代计数器?

#include "stdafx.h"
#include <iostream>

int main(){
    unsigned int cnt = 0;
    char c1;
    while (std::cin >> c1)
        if (c1 == '\t' || c1 == '\n' || c1 == ' ')
            ++cnt;
    std::cout << "The number of spaces,tabs, newlines are: " << cnt << std::endl;

    system("pause");
    return 0;
}

回答by Dietmar Kühl

The way to classify characters in C++ are the functions from <cctype>or the functions from <locale>: there are std::isspace()functions in both of these. Note, that you can only pass positive intvalues to the functions from <cctype>, i.e., to determine if a char cis a space you'd use:

C++中对字符进行分类的方式是函数 from<cctype>或函数 from <locale>:这std::isspace()两者都有函数。请注意,您只能将正值传递int给函数 from <cctype>,即,以确定 achar c是否是您要使用的空格:

if (std::isspace(static_cast<unsigned char>(c))) {
    // c is a space
}

Just using std::isspace(c)result in undefined behavior if charis signed and cis a negative value. On typical systems non-ASCII characters (or multiple bytes of a UTF-8 encoding) use negative values.

std::isspace(c)如果char已签名且c为负值,则仅使用结果会导致未定义的行为。在典型系统上,非 ASCII 字符(或 UTF-8 编码的多个字节)使用负值。

The problems in your original code are:

您原始代码中的问题是:

  • '/n'is not a valid character literal; you probably meant to use '\n'.
  • You omitted a number of space characters, e.g., '\r'(carriage return) and '\v'(vertical space)
  • By default the formatted input operators skip all leading whitespace. To avoid this you can use std::cin >> std::noskipws;. Personally, I would use std::istreambuf_iterator<char>to get the characters. I guess, this is your primary problem.
  • '/n'不是有效的字符文字;您可能打算使用'\n'.
  • 您省略了一些空格字符,例如,'\r'(回车)和'\v'(垂直空格)
  • 默认情况下,格式化输入运算符会跳过所有前导空格。为避免这种情况,您可以使用std::cin >> std::noskipws;. 就个人而言,我会std::istreambuf_iterator<char>用来获取字符。我想,这是你的首要问题。

The pragmatic approach to count spaces is probably something like this:

计算空格的实用方法可能是这样的:

unsigned long spaces = std::count_if(std::istreambuf_iterator<char>(std:cin),
                                     std::istreambuf_iterator<char>(),
                                     [](unsigned char c){ return std::isspace(c); });