C++ 使用退格控制字符擦除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12765297/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:35:44  来源:igfitidea点击:

Erasing using backspace control character

c++escaping

提问by hytriutucx

I am trying to use the backspace control character '\b'to erase trailing commas at the end of line. Although it works in cases where there is no other output to stdout, in case if there is another output after '\b', it becomes useless. Here is an example:

我正在尝试使用退格控制字符'\b'擦除行尾的尾随逗号。虽然它在没有其他输出的情况下有效,但stdout如果在 之后有另一个输出'\b',它就变得无用了。下面是一个例子:

#include <iostream>

using namespace std;

int main()
{
    int a[] =  { 1, 3, 4, 5, 6, 32, 321, 9};
    for ( int i = 0; i < 8; i++) {
        cout << a[i] << "," ;
    }
    cout << "\b" ;
    //cout << endl;
    return 0;
}

In the above block of code, if the line is commented as seen, we get the desired result with no comma after the digit 9. However, if the line uncommented, the comma re-appears.

在上面的代码块中,如果该行被注释为所见,我们将得到所需的结果,数字 9 后没有逗号。但是,如果该行未注释,逗号会重新出现。

In my program, I do not want the comma to be there, but want an endline after 9. How do I do this ?

在我的程序中,我不希望逗号在那里,但希望在 9 之后有一个结束线。我该怎么做?

回答by nneonneo

The usual way of erasing the last character on the console is to use the sequence "\b \b". This moves the cursor back one space, then writes a space to erase the character, and backspaces again so that new writes start at the old position. Note that \bby itself only moves the cursor.

擦除控制台上最后一个字符的常用方法是使用序列"\b \b". 这会将光标向后移动一个空格,然后写入一个空格以擦除字符,然后再次退格以便新的写入从旧位置开始。请注意,\b它本身仅移动光标。

Of course, you could always avoid outputting the comma in the first place:

当然,你总是可以首先避免输出逗号:

if(i > 0) cout << ",";
cout << a[i];

回答by rici

Or, if you're fond of C+11 hacks:

或者,如果您喜欢 C+11 hacks:

adjacent_difference(a.begin(), a.end(), ostream_iterator<int>(std::cout),
  [](int x, int)->int { return std::cout << ",", x; });

回答by Artur

Using backspace escape sequence leads to minor issue. If you want to print your array and you've defined it up front - its size is always non zero. Now imagine that you do not know size of your array, set, list or whatever you want to print and it might be zero. If you've already printed sth. before printing your stuff and you are supposed to print zero elements your backspace will devour something already printed.

使用退格转义序列会导致小问题。如果你想打印你的数组并且你已经预先定义了它 - 它的大小总是非零。现在想象一下你不知道你的数组、集合、列表或任何你想打印的东西的大小,它可能是零。如果你已经打印了某物。在打印你的东西之前,你应该打印零个元素,你的退格键会吞噬已经打印的东西。

Assume you are given pointer to memory location and number of elements to print and use this ...:

假设你得到了指向内存位置和要打印的元素数量的指针,并使用这个......:

void printA(int *p, int count)
{
    std::cout << "elements = [";

    for (int i = 0; i < count; i++)
    {
        std::cout << p[i] << ",";
    }

    std::cout << "\b]\n";
}

...to print:

...打印:

int tab[] = { 1, 2, 3, 4, 5, 6 };

printA(tab, 4);
printA(tab, 0); // <-- a problem

You end up with:

你最终得到:

elements = [1,2,3,4]
elements = ]

In this particular case your opening bracket is 'eaten'. Better not print comma after element and delete last one since your loop may execute zero times and there is no comma to delete. Instead print comma before - yes before each element - but skip first loop iteration - like this:

在这种特殊情况下,您的左括号被“吃掉”了。最好不要在元素后打印逗号并删除最后一个,因为您的循环可能会执行零次并且没有要删除的逗号。而是在每个元素之前打印逗号 - 在每个元素之前是 - 但跳过第一次循环迭代 - 像这样:

void printB(int *p, int count)
{
    std::cout << "elements = [";

    for (int i = 0; i < count; i++)
    {
        if (i != 0) std::cout << ',';
        std::cout << p[i];
    }

    std::cout << "]\n";
}

Now this code:

现在这个代码:

printB(tab, 4);
printB(tab, 0);

generates this:

生成这个:

elements = [1,2,3,4]
elements = []

With backspace esc. seq. you just never know what you might delete.

带退格键。序列 你永远不知道你可能会删除什么。

working example

工作示例

回答by nilbogevoli

yeah the \b will only move the curse so when you end the line it just moves it back to the end of the line. so to actually erase the last part have a space after \b to actually erase the last comma. example: cout<<"\b ";

是的, \b 只会移动诅咒,因此当您结束该行时,它只会将其移回该行的末尾。所以要实际擦除最后一部分在 \b 之后有一个空格来实际擦除最后一个逗号。例如: cout<<"\b ";