如何为可能是c ++代码输出的文本加下划线?
在网络的某个地方我看到了这个:
cout<<underline<<"This is the text which is going to be underlined.";
但是,对我而言,这种“强调”并不起作用。任何想法都非常受欢迎。
答案 0 :(得分:9)
您是否正在输出ANSI终端?如果是这样,以下转义序列应该起作用:
#define underline "\033[4m"
More information on ANSI escape sequences is available here.
注意:要再次关闭下划线,请使用"\033[0m"
。
答案 1 :(得分:5)
可能最简单,最便携的方法就是:
cout << "This is the text which is going to be underlined." << endl;
cout << "-------------------------------------------------" << endl;
答案 2 :(得分:2)
以下是为G ++编写的广泛示例:
#include <iostream>
using namespace std;
int main()
{
char normal[]={0x1b,'[','0',';','3','9','m',0};
char black[]={0x1b,'[','0',';','3','0','m',0};
char red[]={0x1b,'[','0',';','3','1','m',0};
char green[]={0x1b,'[','0',';','3', '2','m',0};
char yellow[]={0x1b,'[','0',';','3', '3', 'm',0};
char blue[]={0x1b,'[','0',';','3','4','m',0};
char Upurple[]={0x1b,'[','4',';','3','5','m',0};
char cyan[]={0x1b,'[','0',';','3','6','m',0};
char lgray[]={0x1b,'[','0',';','3','7','m',0};
char dgray[]={0x1b,'[','0',';','3','8','m',0};
char Bred[]={0x1b,'[','1',';','3','1','m',0};
//for bold colors, just change the 0 after the [ to a 1
//for underlined colors, just change the 0 after the [ to a 4
cout<<"This text is "<<black<<"Black "<<red<<"Red ";
cout<<green<<"Green "<<yellow<<"Yellow "<<blue<<"Blue\n";
cout<<Upurple<<"Underlined Purple "<<cyan<<"Cyan ";
cout<<lgray<<"Light Gray "<<dgray<<"Dark Gray ";
cout<<Bred<<"and Bold Red."<<normal<<"\n";
return 0;
}
答案 3 :(得分:1)
要完成Paul R的回答,我有时会在我的consol程序中创建此功能:
std::string underline(const std::string &s) {
return std::string(s.length(), '-');
}
然后你可以这样做:
int main() {
constexpr auto TEXT = "I am underlined";
std::cout << TEXT << std::endl << underline(TEXT) << std::endl;
return 0;
}
其他可能性:
void underlineAndDisplay(const std::string &s);
std::string underlineWith(const std::string &s, char c);
好吧,让我们回到我的代码......