在 C++ 中,如何将 ASCII 艺术打印到控制台?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2181375/
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
In C++, how to print ASCII art to the console?
提问by Mark
Let's say you want to print out one of those great ASCII art images. How can you do that without cout
each line individually?
假设您想打印出其中一张很棒的 ASCII 艺术图像。如果没有cout
单独的每一行,你怎么能做到这一点?
回答by John Feminella
Adjacent string literals are concatenated, so you can do this:
相邻的字符串文字被连接起来,所以你可以这样做:
cout << " _______________________ _______ _ _______ _______ _______ _______ _ _______ \n"
"( ____ \__ __/ ___ ) ____ \ \ /\ ( ___ )\ /| ____ \ ____ )( ____ \ \ ( ___ )\ /|\n"
"| ( \/ ) ( | ( ) | ( \/ \ / / | ( ) | ) ( | ( \/ ( )|| ( \/ ( | ( ) | ) ( |\n"
"| (_____ | | | (___) | | | (_/ / | | | | | | | (__ | (____)|| (__ | | | | | | | _ | |\n"
"(_____ ) | | | ___ | | | _ ( | | | | ( ) ) __) | __)| __) | | | | | | |( )| |\n"
" ) | | | | ( ) | | | ( \ \ | | | |\ \_/ /| ( | (\ ( | ( | | | | | | || || |\n"
"/\____) | | | | ) ( | (____/\ / \ \ | (___) | \ / | (____/\ ) \ \__| ) | (____/\ (___) | () () |\n"
"\_______) )_( |/ \|_______/_/ \/ (_______) \_/ (_______// \__/|/ (_______/_______)_______)\n";
Or, more accurately, perhaps:
或者,更准确地说,也许:
cout << " .::/- \n"
" .+++/ \n"
" `.::` /+++. \n"
" -////. :+++- \n"
" .////-` .+++/` \n"
" `:///:` `/++/. \n"
" ..` -////. -+++: \n"
" :+++:-` .////:` ./++/` \n"
" `-/+++++/-. `:////.`:++/. \n"
" `.:/++++/:.` -////..:--` \n"
" .-/+++++/-..::.` \n"
" `:::-..`` `.:/++++- \n"
" -++++++///:--.```.-/- \n"
" `.--:///++++++//::. \n"
"`--. ``..-::///+/``--- -+- ./oso- /++: \n"
"-oo+ -::::----....````... `ooo :s- /mo -dmmhy:`hmmo \n"
"-oo+ /+++++++++++++++++/. `ooo om: /mo ```` ``` ``` ``.`` ``` `.`` ommd`` `hmmo ``.`` ``` ``` ``` \n"
"-oo+ ...----::::////+++/` `ooo `/ssyss+:`.ohmyoo` .+ssyss+- -+syys+- /mo -o+. .ohdmmdho- -hdd/ `sdds` :shmmmdy/` .hddshdmmhoydmmmhy:`hmmo .+hdmmmds- .ddd/ .ddh- +ddh. \n"
"-oo+ ``````````````````` `ooo .yh-.``-/- .sm/.` `/o-```-sd+ .sd+-..-++` /mo .odo. :dmmy+/smmm: +mmh- /mmd- +mmh+:/smmy- .dmmdo/+s:`/ymmm++.`hmmo .dmmh++smmd+`ommd` `ymmmy .hmm+ \n"
"-oo+ +oooooooooooooooooo- `ooo -dy. om: -dy` +m/ /mo`+dy- `smmy` `smmy``smms`.hmm/ -dmd+---:hmmo`.dmm+ ommd `hmmo ommh. ommh..ymm+ +mmdmm/ ommy. \n"
"-oo+ /++++++++++++++++++. `ooo -oyhyyys/` om: `:osyyyyymy``sm- /myhyhd: `smms +mmh` `dmm/smms :dmmddddddddo`.dmm/ ommd `hmmo smmy` /mmd. :dmd+dmy-ymd+hmd: \n"
"-oo+ `ooo ``.+do om: /do. -dy``om: /md/``od+` `ommh. `ymmy` :dmmmmy. .hmd/`````.` .dmm/ ommd hmmo +mmh- smmy` `smmmmm- :dmmmmo \n"
"-oo+:::::::::::::::::::::::/ooo -+:.```.od+ +mo.` /do.```.omy` .sd/.``.//` /mo +dy. -ymmdysdmmh- +mmmh- :dmmyoosdd+` .dmm/ ommd ommmso.`ymmdyshmmh: .hmmm+ +mmmd` \n"
"-oooooooooooooooooooooooooooooo ./syyyyyo:` `:sys.`:syyyys+yo` `:syyyyo:` :h/ :ys` `:shddhs/` `ohy/ ./shddhy+- .shh: /hhy `:syhs. `:oyhdhs/. /hho` `shh/ \n"
More sensibly, use endl
. This is subtly different from just "\n" after each line, because you'll also flush the output stream.
更明智的是,使用endl
. 这与每行后面的 "\n" 略有不同,因为您还将刷新输出流。
回答by Neil Roy
try something like:
尝试类似:
cout << R"(place multiple lines
of text here
and it will display exactly
as you have it between the two brackets,
line feeds and all.)";
...the above code will also allow you to use characters like the backslash \ without needing two of them, it displays everything and doesn't recognize control codes, like \n etc. Very handy.
...上面的代码还允许您使用像反斜杠 \ 这样的字符,而不需要其中的两个,它显示所有内容并且不识别控制代码,如 \n 等。非常方便。
This is called a "string literal" and was added in C++11. You can find more information on the commands here, specifically refer to the prefix "R" which is for raw_characters: https://en.cppreference.com/w/cpp/language/string_literal
这称为“字符串文字”,是在 C++11 中添加的。您可以在此处找到有关命令的更多信息,特别是指用于 raw_characters 的前缀“R”:https://en.cppreference.com/w/cpp/language/string_literal
回答by Jerry Coffin
Others have already suggested using endl
. While this isn't (necessarily) a bad thing, using endl
flushes the stream's buffer along with writing a new-line. Contrary to the implication in one of the answers you've gotten, using endl
does nothelp (at all) with translating the new-line to whatever character sequence the platform normally uses to signal the end of a line. Using newline
is guaranteed to be precisely equivalent to stream << "\n" << flush;"
. Translating new-lines to "\r", or "\n" or "\r\n", or whatever the platform prefers, is done at a different level and newline
has nothing to do with it.
其他人已经建议使用endl
. 虽然这不是(必然)坏事,但使用endl
刷新流的缓冲区并写入换行符。相反,在你得到的答案之一的寓意,用endl
做不是帮助(全部)与翻译换行到任何字符序列的平台通常使用的信号线的末端。Usingnewline
保证完全等同于stream << "\n" << flush;"
. 将换行符转换为 "\r"、"\n" 或 "\r\n",或者平台喜欢的任何内容,都是在不同的级别完成的,newline
与此无关。
The flush
that it does, however, can (and often will) slow down your I/O, sometimes by quite a considerable margin. As long as you're only writing a few lines (e.g. a couple hundred characters) it's probably completely irrelevant. If you're writing a large file, however, using endl
instead of "\n"
can easily result in a 10x slowdown (in fact, I'd go so far as to say that much of the idea that iostreams are slow stems directly from using endl
).
然而flush
,它确实会(并且经常会)减慢您的 I/O,有时会降低相当大的幅度。只要您只写几行(例如几百个字符),它可能完全无关紧要。但是,如果您正在编写一个大文件,使用endl
而不是"\n"
很容易导致 10 倍的速度变慢(事实上,我什至可以说 iostream 很慢的大部分想法直接源于使用endl
)。
That's not to say there's never any reason to use endl. The flush
assures that whatever has been written to the stream is immediately flushed out of the standard library's buffer, and sent to the OS. If you want to assure immediate display, endl
can be useful. Likewise, if you're doing logging, and it's critical that your log always reflect the most recent known state of a program, endl
can be (extremely) useful to assure that what you've written really gets logged, not lost in a buffer when/if the application crashes.
这并不是说永远没有任何理由使用 endl。的flush
,无论已写入流保证立即冲出标准库的缓冲,并将其发送到OS。如果你想保证立即显示,endl
会很有用。同样,如果您正在进行日志记录,并且您的日志始终反映程序的最新已知状态至关重要,endl
则可以(非常)有用地确保您编写的内容真正被记录下来,而不是丢失在缓冲区中/如果应用程序崩溃。
So, endl
makes sense at times, but probably 95% of the time that it's used, it's really inappropriate (e.g., it's unlikely to accomplish anything useful in any of the answers to this question).
所以,endl
有时是有道理的,但可能 95% 的时间使用它,它真的不合适(例如,它不太可能在这个问题的任何答案中完成任何有用的事情)。
回答by Player1st
It's pretty simple thankfully. Just use endl whenever you want to start another line.
谢天谢地,这很简单。每当您想开始另一行时,只需使用 endl 。
cout << stuff << endl
<< morestuff << endl
<< evenmorestuff << endl;
I would like to state that I much prefer using endl because it should work even if you are on a platform that requires "\r\n" instead of just "\n".
我想声明我更喜欢使用 endl,因为即使您在需要“\r\n”而不仅仅是“\n”的平台上,它也应该可以工作。