C++ 中的粗体输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29997096/
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
Bold output in C++
提问by KitKat
I'm building a dictionary, and when I print(output) the word-defenitions I'd like to print the word itself in bold. when I print
我正在构建一本字典,当我打印(输出)单词定义时,我想以粗体打印单词本身。当我打印
cout<<word<<endl<<defention1<<defenition2<<endl;
I want the only "word" to be bold.
我希望唯一的“单词”是粗体的。
How can I do that?
我怎样才能做到这一点?
回答by vsoftco
Standard C++ uses various locales/character sets to display the output in various alphabets. However, the text itself is just that, text, without formatting.
标准 C++ 使用各种语言环境/字符集以各种字母表显示输出。但是,文本本身就是文本,没有格式。
If you want your output to be
colored
, orbold
, oritalicized
, then you need to send an appropriate character code to your terminal.
如果您希望输出为
colored
, orbold
, oritalicized
,则需要向终端发送适当的字符代码。
However, this is implementation-defined and not guaranteedto work on all platforms.
但是,这是实现定义的,并不能保证在所有平台上都有效。
For example, in Linux/UNIX you may use ANSI escape codesif your terminal supports them.
例如,在 Linux/UNIX 中,如果您的终端支持,您可以使用ANSI 转义码。
Example that works on my Mac OS X:
适用于我的 Mac OS X 的示例:
#include <iostream>
int main()
{
std::cout << "\e[1mBold\e[0m non-bold" << std::endl; // displays Bold in bold
}
If you want, you can go an extra step and create manipulators for turning on/off the bold-ing:
如果你愿意,你可以多走一步,创建操纵器来打开/关闭粗体:
#include <iostream>
std::ostream& bold_on(std::ostream& os)
{
return os << "\e[1m";
}
std::ostream& bold_off(std::ostream& os)
{
return os << "\e[0m";
}
int main()
{
std::cout << bold_on << "bold" << bold_off << " non-bold" << std::endl;
}
回答by FalconUA
The standart c++
can't output the text with any formatting. However, it's possible to output your string in bold, and even in different colours.
It depends on the operation system you're using and the terminal/console you're running in.
标准c++
不能输出任何格式的文本。但是,可以以粗体甚至不同颜色输出您的字符串。这取决于您使用的操作系统和您运行的终端/控制台。
For example, in Window'sconsole, there's no way to write a text in bold.
If you're using Linux/Unix, then, in most terminal emulators and in virtual console, you can write your string in bold, and even choose the color for it, just by adding \e[1m
before your string, and \e[0m
after your string to make sure that the other strings will be not bold.
例如,在Window 的控制台中,无法以粗体书写文本。
如果您使用的是Linux/Unix,那么在大多数终端模拟器和虚拟控制台中,您可以将字符串以粗体书写,甚至可以为其选择颜色,只需\e[1m
在字符串之前和\e[0m
字符串之后添加以确保其他字符串不会是粗体。
\e
is the escape symbol. In Vim, you can simply write it just by pressing ctrl + v + esc
.
\e
是转义符。在 Vim 中,您只需按 即可编写它ctrl + v + esc
。
Here is a simple example for Linux/Unix (Mac is also Unix):
这是 Linux/Unix 的一个简单示例(Mac 也是 Unix):
char esc_char = 27; // the decimal code for escape character is 27
cout << esc_char << "[1m" << "Hello Bold!" << esc_char << "[0m" << endl;
回答by Jerry Coffin
While I like the idea of a manipulator to bold some text (as @vsoftco has shown) I'm not particularly fond of the manipulators he has. Personally, I'd prefer the code look something like this:
虽然我喜欢使用操纵器来加粗某些文本的想法(如@vsoftco 所示),但我并不是特别喜欢他拥有的操纵器。就个人而言,我更喜欢代码如下所示:
std::cout << bold(word) << definition1 << definition2;
This avoids having a long-term state of the stream that needs to be manipulated. In doing so, it avoids the possibility of somebody manipulating the state incorrectly, such as leaving bolding active when that wasn't desired.
这避免了需要操作的流的长期状态。这样做可以避免有人错误地操纵状态的可能性,例如在不希望的情况下保留粗体活动。
To support that, I'd write the manipulator more like this:
为了支持这一点,我会更像这样编写操纵器:
class bold {
std::string_view const &s;
public:
bold(std::string_view const &s) : s(s) {}
friend std::ostream &operator<<(std::ostream &os, bold const &b) {
os << "\x1b[1m" << b.s << "\x1b[0m";
}
};
Then we can write code using the manipulator like this:
然后我们可以像这样使用操纵器编写代码:
int main() {
std::cout << bold("word") << " " << "definition\n";
}
I do hasten to add, however, that this is something of a stylistic choice. In particular, if you were starting with something on this order:
然而,我确实赶紧补充说,这是一种风格选择。特别是,如果您从以下订单开始:
std::cout << bold_on << a << b << c << d << bold_off;
...where a
, b
, c
and d
are all different types, then @Vsoftco's approach will almost certainly be more attractive than this one.
... where a
, b
, c
andd
都是不同的类型,那么@Vsoftco 的方法几乎肯定会比这个更有吸引力。