C ++中的彩色输出 [英] Colored output in C++

查看:91
本文介绍了C ++中的彩色输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用iostream和Xcode打印彩色输出?我希望能够例如用Hello红色,World蓝色和!黄色打印Hello World!.我该怎么办?

Is there a way to print colored output using iostream and Xcode? I'd like to be able to, for example, print Hello World! with Hello red, World blue and ! yellow. How can I do that?

推荐答案

您需要终端颜色代码.对于linux,它是以下内容(您的系统可能有所不同,请查找它):

You need the terminal color codes. For linux it's the following (your system might be different, look it up):

//the following are UBUNTU/LINUX, and MacOS ONLY terminal color codes.
#define RESET   "\033[0m"
#define BLACK   "\033[30m"      /* Black */
#define RED     "\033[31m"      /* Red */
#define GREEN   "\033[32m"      /* Green */
#define YELLOW  "\033[33m"      /* Yellow */
#define BLUE    "\033[34m"      /* Blue */
#define MAGENTA "\033[35m"      /* Magenta */
#define CYAN    "\033[36m"      /* Cyan */
#define WHITE   "\033[37m"      /* White */
#define BOLDBLACK   "\033[1m\033[30m"      /* Bold Black */
#define BOLDRED     "\033[1m\033[31m"      /* Bold Red */
#define BOLDGREEN   "\033[1m\033[32m"      /* Bold Green */
#define BOLDYELLOW  "\033[1m\033[33m"      /* Bold Yellow */
#define BOLDBLUE    "\033[1m\033[34m"      /* Bold Blue */
#define BOLDMAGENTA "\033[1m\033[35m"      /* Bold Magenta */
#define BOLDCYAN    "\033[1m\033[36m"      /* Bold Cyan */
#define BOLDWHITE   "\033[1m\033[37m"      /* Bold White */

这允许您执行以下操作:

This allows you to do the following:

std::cout << RED << "hello world" << RESET << std::endl;

注意:如果不使用RESET,颜色将保持更改,直到下次使用颜色代码为止.

Note: If you don't use RESET the color will remain changed until the next time you use a color code.

这篇关于C ++中的彩色输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆