C++ 尾随空格是什么意思,它和空格有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22273233/
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 00:01:39  来源:igfitidea点击:

what is meant by trailing space and what's the difference between it and a blank?

c++c

提问by user12448

What is meant by trailing space and what's the difference between it and a blank ? I saw an exercise where there is a note about trailing space.

尾随空格是什么意思,它和空格有什么区别?我看到一个练习,其中有关于尾随空格的注释。

I didn't see the problem on that because cincan ignore those spaces and catch only numbers ?

我没有看到问题,因为cin可以忽略这些空格并只捕获数字?

回答by LeonardBlunderbuss

Trailing space is all whitespacelocated at the end of a line, without any other characters following it. This includes spaces (what you called a blank) as well as tabs \t, carriage returns \r, etc. There are 25 unicode characters that are considered whitespace, which are listed on Wikipedia.

尾随空格是位于行尾的所有空白,其后没有任何其他字符。这包括空格(您称之为空格)以及制表符\t、回车符\r等。有 25 个 unicode 字符被视为空格,列在Wikipedia 上

回答by 2785528

what's the difference between [trailing space] and a blank?

[尾随空格]和空格有什么区别?

A blank at the end of a line is a trailing space. A blank between characters (or words, or digits) is not a trailing space.

行尾的空白是尾随空格。字符(或单词或数字)之间的空格不是尾随空格。

what is meant by trailing space?

尾随空格是什么意思?

Trailing space became a challenge for me for something I was trying to code. The challenge inspired me to create the following utility routines. For this particular effort, I defined "trailing space" as any "white space" at the end of a line. (Yes, I also created versions of this function for leading white space, and extra white space (more than 1 white space character in the middle of the line.)

对于我试图编码的东西,拖尾空间对我来说是一个挑战。这个挑战激励我创建以下实用程序。对于这项特殊的工作,我将“尾随空间”定义为行尾的任何“空白”。(是的,我还创建了这个函数的版本,用于前导空格和额外的空格(行中间超过 1 个空格字符。)

const char* DTB::whitespaces = "\t\n\v\f\r ";
//                               0 1 2 3 4 5
// 0)tab, 1)newline, 2)vertical tab, 3)formfeed, 4)return, 5)space,


void DTB::trimTrailingWhiteSpace(std::string& s)
{
   do // poor man's try block
   {
      if(0 == s.size())  break;  // nothing to trim, not an error or warning

      // search from end of s until no char of 'whitespaces' is found
      size_t found = s.find_last_not_of(DTB::whitespaces);

      if(std::string::npos == found)  // none found, so s is all white space
      {
         s.erase(); // erase the 'whitespace' chars, make length 0
         break;
      }

      // found index to last not-whitespace-char
      size_t trailingWhitespacesStart = found + 1; // point to first of trailing whitespace chars
      if(trailingWhitespacesStart < s.size())      // s has some trailing white space
      {
         s.erase(trailingWhitespacesStart); // thru end of s
         break;
      }

   }while(0);

} // void trimTrailingWhiteSpace(std::string& s)

回答by Marco A.

A trailing space in programming (as I think you're referring) it's a series of whitespaces at the end of a string or a line.

编程中的尾随空格(我认为您指的是)它是字符串或行末尾的一系列空格。

They can cause some hassle under the following circumstances:

在以下情况下,它们可能会引起一些麻烦:

  • You might have a string literal spanning multiple lines, in that case it can be tricky to debug a trailing whitespace

  • Can slow the development process when you always have to "fix" those by hand when you have to type-append on a line

  • Some parsing tools might have problems with them

  • 您可能有一个跨越多行的字符串文字,在这种情况下,调试尾随空格可能会很棘手

  • 当您必须在一行上键入附加时,您总是必须手动“修复”它们,这可能会减慢开发过程

  • 一些解析工具可能有问题