如何输出有符号变量

问题描述:

如何打印变量的有符号值?例如:

How do I print signed value of my variable? For example:

int main()
{
    int variable_name = 2;
    cout<<variable_name<<endl;
}

我希望这段代码打印 +2,而不仅仅是 2.而且我不想使用这样的结构:

I want this code to print +2, not just 2. And I don't want to use such construction:

cout<<"+"<<variable_name<<endl;

std::showpos 正是您要找的:

http://www.cplusplus.com/reference/ios/showpos/