如何删除放到std :: cout的最后一个字符?

时间:2022-07-01 05:49:08

Is it possible on Windows without using WinAPI?

是否可以在不使用WinAPI的Windows上使用?

3 个解决方案

#1


67  

You may not remove last character.

您可能无法删除最后一个字符。

But you can get the similar effect by overwriting the last character. For that, you need to move the console cursor backwards by outputting a '\b' (backspace) character like shown below.

但是你可以通过覆盖最后一个字符来获得类似的效果。为此,您需要通过输出如下所示的'\ b'(退格)字符向后移动控制台光标。

#include<iostream>
using namespace std;
int main()
{
    cout<<"Hi";
    cout<<'\b';  //Cursor moves 1 position backwards
    cout<<" ";   //Overwrites letter 'i' with space
}

So the output would be

所以输出就是

H

H

#2


6  

No.

没有。

You can't without accessing the console's api that is never standard.

你不能没有访问控制台的api,这是永远不会标准的。

#3


-1  

This code does exactely that std::cout<<"\b \b";

这段代码确实是std :: cout <<“\ b \ b”;

#1


67  

You may not remove last character.

您可能无法删除最后一个字符。

But you can get the similar effect by overwriting the last character. For that, you need to move the console cursor backwards by outputting a '\b' (backspace) character like shown below.

但是你可以通过覆盖最后一个字符来获得类似的效果。为此,您需要通过输出如下所示的'\ b'(退格)字符向后移动控制台光标。

#include<iostream>
using namespace std;
int main()
{
    cout<<"Hi";
    cout<<'\b';  //Cursor moves 1 position backwards
    cout<<" ";   //Overwrites letter 'i' with space
}

So the output would be

所以输出就是

H

H

#2


6  

No.

没有。

You can't without accessing the console's api that is never standard.

你不能没有访问控制台的api,这是永远不会标准的。

#3


-1  

This code does exactely that std::cout<<"\b \b";

这段代码确实是std :: cout <<“\ b \ b”;