C++ 如何删除放入 std::cout 的最后一个字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3745861/
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 to remove last character put to std::cout?
提问by Xirdus
Is it possible on Windows without using WinAPI?
是否可以在不使用 WinAPI 的情况下在 Windows 上运行?
回答by bjskishore123
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
回答by Klaim
No.
不。
You can't without accessing the console's api that is never standard.
你不能不访问从来没有标准的控制台的 api。
回答by Hassen Dhia
This code does exactely that
std::cout<<"\b \b";
这段代码正是这样做的
std::cout<<"\b \b";
回答by Touka
You can also use cin.get()
to delete last char
您还可以使用cin.get()
删除最后一个字符