什么是尾随空格,与空格之间有什么区别?
我看到了一道关于尾随空格的练习。
我不认为这个问题有什么影响,因为 cin
可以忽略那些空格,并且只捕获数字?
尾随空格是指位于行末的所有 空白字符,没有其他字符跟在它后面。这包括空格(你所说的空格)以及制表符\t
、回车\r
等。有25个Unicode字符被视为空白字符,在维基百科上列出了这些字符。(链接)
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)
在编程中,拖尾空格(我想你指的是这个)是指字符串或行末的一系列空格。
以下情况下它们可能会引起一些麻烦:
您可能有一个跨越多行的字符串字面量,在这种情况下,调试拖尾空格可能会很棘手
当您必须在一行上输入附加内容时,总是需要手动“修复”这些内容,这会减缓开发过程
一些解析工具可能会遇到问题
cin
会忽略末尾的空格。 - LeonardBlunderbusscin
如何“忽略尾随空格”,同时又能确定行末,如果它忽略了包括'\n'
在内的空格?难道空格的相关性不是由使用cin
的函数而不是cin
本身决定的吗? - chux - Reinstate Monica