Learning C++ 之1.6 空格和格式

空格是一类用来调整格式的字符。在C++中这个是由space,tabs或者newlines来控制的。C++一般是忽略掉空格的,除非一些非常特殊的的情况。

通常来说下面的语句都是一个意思:

std::cout << "Hello world!";
 
std::cout               <<            "Hello world!";
 
		std::cout << 		"Hello world!";
 
std::cout
	<< "Hello world!";

尽管最后一个都换行了,但是仍然能够正常执行。

下面的函数做的都是同一件事情:

int add(int x, int y) { return x + y; }
 
int add(int x, int y) {
    return x + y; }
 
int add(int x, int y)
{    return x + y; }
 
int add(int x, int y)
{
    return x + y;
}

当C++语言唯一识别的空格是在“”里面的空格,如:

"Hello world!"

和下面的:

"Hello         world!"

是不同的。

这种事严格符合你的打印的。

切记,换行是不能在“”里面的。

std::cout << "Hello
     world!" << std::endl; // Not allowed!

上面的语法是错误的,会导致编译不过。

另一种需要注意的是,空格在//的后面。一定要注意,换行之后注释就不起作用了。

std::cout << "Hello world!" << std::endl; // Here is a single-line comment
this is not part of the comment

基础格式:

不像其他一些语言,C++是不会强制程序语言使用一种格式的,C++相信编程者。一些C++的书写格式就被创造出来了,而且你也很难说哪一种是好的。我们基本的原则是保持语言的易读性,以及一致性。

下面是我们的一些建议:

1.tabs应该用4个空格键替代,一些IDE使用3个空格,虽然有些不同寻常,但是也可以使用。

你可以使用tabs或者space来断行,但是建议使用space,两个或者四个space。因为这会是所有的IDE保持相同的格式,方便移植编辑。

2.{}表示一个函数的开始和结束,应该和函数名对齐,在新的一行

int main()
{
}

3.大括号开始的行都是以四个空格开始或者tabs开头

#include <iostream>
int main()
{
    std::cout << "Hello world!" << std::endl; // tabbed in one tab (4 spaces)
    std::cout << "Nice to meet you." << std::endl; // tabbed in one tab (4 spaces)
}

4.行数不能太长,一般最多需要72,78或者80个字符。如果一行的字符比较多,那么可以考虑拆分成多行,如下:

#include <iostream>
int main()
{
    std::cout << "This is a really, really, really, really, really, really, really, " <<
        "really long line" << std::endl; // one extra indentation for continuation line
 
    std::cout << "This is another really, really, really, really, really, really, really, " <<
                 "really long line" << std::endl; // text aligned with the previous line for continuation line
 
    std::cout << "This one is short" << std::endl;
}

如果一个长行被操作符隔开,那么操作符应该放在橘子的最后边,而不是最前边:

    std::cout << "This is a really, really, really, really, really, really, really, " <<
            "really long line" << std::endl;

而不是:

    std::cout << "This is a really, really, really, really, really, really, really, "
            << "really long line" << std::endl;

这种写法更容易让人联想到第二行是第一行的延续。

5.用空格让你的代码更容易读

难以读的:

nCost = 57;
nPricePerItem = 24;
nValue = 5;
nNumberOfItems = 17;

容易读的:

nCost          = 57;
nPricePerItem  = 24;
nValue         = 5;
nNumberOfItems = 17;

难以读的:

std::cout << "Hello world!" << std::endl; // std::cout and std::endl live in the iostream library
std::cout << "It is very nice to meet you!" << std::endl; // these comments make the code hard to read
std::cout << "Yeah!" << std::endl; // especially when lines are different lengths

容易读的:

std::cout << "Hello world!" << std::endl;                  // std::cout and std::endl live in the iostream library
std::cout << "It is very nice to meet you!" << std::endl;  // these comments are easier to read
std::cout << "Yeah!" << std::endl;                         // especially when all lined up

我们将这些建议用在这片教材里,当然希望着会成为你的一个好习惯。我们在介绍给你新的主题的时候,我们再来介绍新的建议配合新的格式。

最后,C++会给你自己选择适合自己的格式,但是还是建议使用上面介绍的一种。因为大家都是用一样的格式,这样就方便代码的移植与维护了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值