C++ 制表符(\t)有多少个空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13094690/
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
How many spaces for tab character(\t)?
提问by Chao Zhang
I want to implement a text drawing function. But I am not sure how \t
works, which means I don't know how many spaces I should print for \t
.
我想实现一个文字绘制功能。但我不确定如何\t
工作,这意味着我不知道我应该为\t
.
I have come up with the following algorithm:
我提出了以下算法:
a) Each \t
represents at most NUMBER_OF_SPACES_FOR_TAB
spaces.
b) If \t
appears in the last line at a corresponding position, \t
for this line should be aligned to the \t
of last line.
a) 每个\t
最多代表一个NUMBER_OF_SPACES_FOR_TAB
空格。b) 如果\t
出现在最后一行的相应位置,则\t
此行应与\t
最后一行的对齐。
Example:
例子:
printf("a\t\tb\n");
printf("\t\tc\n");
Should print:
应该打印:
a11112222b
34444c
Where:
在哪里:
1.Number i
represents the spaces of \t
at position i
1.Numberi
表示\t
at位置的空格i
2.NUMBER_OF_SPACES_FOR_TAB == 4
2.NUMBER_OF_SPACES_FOR_TAB == 4
Does anyone know the standard algorithm? Thanks in advance.
有谁知道标准算法?提前致谢。
回答by Mark Ransom
A tab character should advance to the next tab stop. Historically tab stops were every 8th character, although smaller values are in common use today and most editors can be configured.
制表符应前进到下一个制表位。从历史上看,制表位是每 8 个字符一个,尽管今天普遍使用较小的值,并且大多数编辑器都可以配置。
I would expect your output to look like the following:
我希望您的输出如下所示:
123456789
a b
c
The algorithm is to start a column count at zero, then increment it for each character output. When you get to a tab, output n-(c%n)
spaces where c
is the column number (zero based) and n
is the tab spacing.
该算法是从零开始列计数,然后为每个字符输出增加它。当您到达制表符时,输出n-(c%n)
空格其中c
是列号(从零开始)和n
制表符间距。
回答by John Kugelman
Imagine a ruler with tab stops every 8 spaces. A tab character will align text to the next tab stop.
想象一下每 8 个空格带有制表位的标尺。制表符会将文本与下一个制表位对齐。
0 8 16 24 32 40
|.......|.......|.......|.......|.......|
printf("\tbar\n"); \t bar
printf("foo\tbar\n"); foo\t bar
printf("longerfoo\tbar"); longerfoo\t bar
To calculate where the next tab stop is, take the current column
.
要计算下一个制表位的位置,请获取当前的column
.
nextTabStop = (column + 8) / 8 * 8
The / 8 * 8
part effectively truncates the result to the nearest multiple of 8. For example, if you're at column 11, then (11 + 8) is 19 and 19 / 8 is 2, and 2 * 8 is 16. So the next tab stop from column 11 is at column 16.
该/ 8 * 8
部分有效地将结果截断为最接近的 8 倍数。例如,如果您在第 11 列,则 (11 + 8) 是 19,19 / 8 是 2,2 * 8 是 16。所以下一个选项卡从第 11 列停止是在第 16 列。
In a text editor you may configure tab stops to smaller intervals, like every 4 spaces. If you're simulating what tabs look like at a terminal you should stick with 8 spaces per tab.
在文本编辑器中,您可以将制表位配置为较小的间隔,例如每 4 个空格。如果您正在模拟终端上的选项卡是什么样的,您应该坚持每个选项卡有 8 个空格。
回答by Whyrusleeping
A Tab character shifts over to the next tab stop. By default, there is one every 8 spaces. But in most shells you can easily edit it to be whatever number of spaces you want (profile preferences in linux, set tabstop in vim).
制表符移到下一个制表位。默认情况下,每 8 个空格有一个。但是在大多数 shell 中,您可以轻松地将其编辑为您想要的任意数量的空格(在 linux 中配置文件首选项,在 vim 中设置 tabstop)。