如何在C++中使用前缀+打印正数

Sri*_*ath 2 c++ int

有没有办法打印整数及其在c ++中的符号...即默认情况下,如果数字是负数,我们会-打印一个符号.同样,我们可以+在正数之前得到.

int x=-1;
cout<<"x="<<x;
Run Code Online (Sandbox Code Playgroud)

给出输出 x=-1

但,..

int x=+1;
cout<<"x="<<x;
Run Code Online (Sandbox Code Playgroud)

给出输出x=1但是如何将其打印为x=+1

我知道我们可以通过使用if-elsefor x> 0和x <0; ..而不使用if-else,在c ++中有任何直接的打印方式

NPE*_*NPE 9

用途std::showpos:

int x = 1;
std::cout << "x=" << std::showpos << x;
Run Code Online (Sandbox Code Playgroud)