C++ 你如何使用 cout 输出 \ 符号?

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

How do you output the \ symbol using cout?

c++

提问by SomeUser

How do you output \ symbol using cout?

你如何使用 cout 输出 \ 符号?

回答by Daniel A. White

Use two backslashes \\

使用两个反斜杠 \\

回答by Tom

In addition to all the correct answers, see this for further escaped characters

除了所有正确答案外,请参阅此以了解更多转义字符

\a  Bell (beep)
\b  Backspace
\f  Formfeed
\n  Newline
\r  Return
\t  Tab
\  Backslash
\'  Single quote
\"  Double quote
\xdd    Hexadecimal representation
\ddd    Octal representation
\?  Question mark ('?')

回答by mch

The '\' character is an escape character in C and C++. You can output a literal '\' by escaping it with itself:

'\' 字符是 C 和 C++ 中的转义字符。您可以通过将其转义来输出文字 '\':

   cout << "This is a backslash: \";

回答by SomeUser

Maybe

也许

cout << "\";

回答by sbi

std::cout << '\';

回答by Dharman

Since C++11 you can also use raw string literals.

从 C++11 开始,您还可以使用原始字符串文字

std::cout << R"(There is no need to escape backslash here \)";

Escape characters (like \n \t or " ) are not processed in the raw string literals.

未在原始字符串文字中处理转义字符(如 \n \t 或 " )。

回答by Mr_Tanki_Online_Pro_TO

cout << "\" << endl;

Instead of endl you could:

而不是 endl 你可以:

cout << "\ \n";