可以分为两种情况:
- 代码换行
- 长字符串换行
一、 C/C++代码换行直接用enter键换行,同时注意关键字不要切割:
bool a=true;
if(a
==
true)
{
std::cout<<"Hello"<<std::endl;
}//ok,关键字没有切割
if(a
=
=
true)
{
std::cout<<"Hello"<<std::endl;
}//error,切分了关键字==
下面可以但是最好不要这么做的用法(可读性):
void fun\
ction()
{
std::cout<<"I am function"<<std::endl;
}
宏函数倒是挺常见的:
#define MAX(a,b)\
((a)>(b)?(a):(b))
二、长字符串换行
两种方式实现长字符串在编辑区换行。
2.1 双引号
将一个大字符串拆分成属于不同行的、引号字符串。
const char *text =
"This text is pretty long, but will be "
"concatenated into just a single string. "
"The disadvantage is that you have to quote "
"each part, and newlines must be literal as "
"usual.";
这种情况下,每一行右侧引号左侧的缩进无需考虑,因为它不属于这个长字符串。
2.2 反斜杠
const char *text2 =
"Here, on the other hand, I've gone crazy \
and really let the literal span several lines, \
without bothering with quoting each line's \
content. This works, but you can't indent.";
因为字符串是一个整体,所以必须要用反斜杠\
。
2.3 Raw-string支持
在C++11以后,支持了一种所见即所得的一种方式。
const char * vogon_poem = R"(
O freddled gruntbuggly thy micturations are to me
As plured gabbleblochits on a lurgid bee.
Groop, I implore thee my foonting turlingdromes.
And hooptiously drangle me with crinkly bindlewurdles,
Or I will rend thee in the gobberwarts with my blurlecruncheon, see if I don't.
)";
std::cout<<vogon_poem<<std::endl;
[1] https://stackoverflow.com/questions/1135841/c-multiline-string-literal