假设我有这样的代码:
void printHex(std::ostream& x){
x<<std::hex<<123;
}
..
int main(){
std::cout<<100; // prints 100 base 10
printHex(std::cout); //prints 123 in hex
std::cout<<73; //problem! prints 73 in hex..
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,在从函数返回后,是否有任何方法可以将cout的状态"恢复"到原来的状态?(有点像std :: boolalpha和std :: noboolalpha ..)?
谢谢.
我想在C++中使用无符号的8位变量.关于算术的任何一个unsigned char或者uint8_t做的伎俩(这是预期的,因为AFAIK uint8_t只是一个别名unsigned char,或者调试器提出它.
问题是,如果我在C++中使用ostream打印出变量,它会将其视为char.如果我有:
unsigned char a = 0;
unsigned char b = 0xff;
cout << "a is " << hex << a <<"; b is " << hex << b << endl;
Run Code Online (Sandbox Code Playgroud)
然后输出是:
a is ^@; b is 377
Run Code Online (Sandbox Code Playgroud)
代替
a is 0; b is ff
Run Code Online (Sandbox Code Playgroud)
我尝试过使用uint8_t,但正如我之前提到的,那是typedef'ed unsigned char,所以它也是如此.如何正确打印变量?
编辑:我在我的代码中的许多地方都这样做.有没有什么办法可以做到这一点,而不铸造int我想打印每一次?
如何在C++中格式化输出?换句话说,什么是C++相当于使用printf这样的:
printf("%05d", zipCode);
Run Code Online (Sandbox Code Playgroud)
我知道我可以printf在C++中使用,但我更喜欢输出运算符<<.
你会使用以下内容吗?
std::cout << "ZIP code: " << sprintf("%05d", zipCode) << std::endl;
Run Code Online (Sandbox Code Playgroud) 如何使用cout打印0x0a而不是0xa?
#include <iostream>
using std::cout;
using std::endl;
using std::hex;
int main()
{
cout << hex << showbase << 10 << endl;
}
Run Code Online (Sandbox Code Playgroud) 我一直在尝试将输出格式化为控制台的时间最长,并且没有发生任何事情.我一直在尝试尽可能多地使用iomanip和ofstream&out函数.
void list::displayByName(ostream& out) const
{
node *current_node = headByName;
// I have these outside the loop so I don't write it every time.
out << "Name\t\t" << "\tLocation" << "\tRating " << "Acre" << endl;
out << "----\t\t" << "\t--------" << "\t------ " << "----" << endl;
while (current_node)
{
out << current_node->item.getName() // Equivalent tabs don't work?
<< current_node->item.getLocation()
<< current_node->item.getAcres()
<< current_node->item.getRating()
<< endl;
current_node = current_node->nextByName;
}
// The equivalent tabs do …Run Code Online (Sandbox Code Playgroud) 在C++程序中,我想显示一列浮点值,以便符号,数字和小数点都排成一行.必要时,多个前导零应填充每个值的整数部分.例如:
A column of floating point values:
+000.0012
-000.0123
+000.1235
-001.2346
+012.3457
-123.4568
Run Code Online (Sandbox Code Playgroud)
我有一个精心评论的测试程序,证明了这个问题.但是,当我编辑这篇文章时,我在这里找到了我需要的答案:
- 使用printf打印浮点数时,额外的前导零?
基本的问题是我使用的格式代码"%+04.4f"应该使用"%+09.4f",因为我想要的总字段宽度为9:
我没有足够的声誉点来评论该帖子,所以谢谢你,@ AndiDog.
我仍然不知道如何使用流格式化标志获得多个前导零.但这是另一天的战斗.我现在会坚持使用printf和stream的混合物.
在C++中是否有一种方法可以使编译器获取一定数量的数字,即使它们的第一个数字是0.例如:
我有一个00001的项目编号,当我从文件中导入数字时,它显示1.我希望它导入所有五位数字并显示为00001.
我真的没有要显示的代码,因为我甚至不知道使用什么函数,而且我所拥有的代码正如宣传的那样工作,这不是我想要的.我可以将数字设为字符串,但我宁愿保持整数.
我正在尝试将数值写入与列对齐的文本文件中.我的代码看起来像这样:
ofstream file;
file.open("try.txt", ios::app);
file << num << "\t" << max << "\t" << mean << "\t << a << "\n";
Run Code Online (Sandbox Code Playgroud)
它有效,除非值没有相同的数字,否则它们不对齐.我想要的是以下内容:
1.234567 -> 1.234
1.234 -> 1.234
1.2 -> 1.200
Run Code Online (Sandbox Code Playgroud) 我已经查看过该线程,但如果我使用 setfill( '0' ),它不会考虑使用负数。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//Code
int num1;
cin >> num1;
cout << setw(5) << setfill( '0' ) << num1 << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用此方法,如果我输入“5”,它将显示为“00005”。但如果我输入 -5 它将显示为“000-5”
我该如何修复它以使输出为“-0005”?
把这些问题放在一起:
使用cout << operator时,如何使用前导零填充int?
我如何流式传输到std :: cout,例如,这个变量
double x = 7.1224
Run Code Online (Sandbox Code Playgroud)
让它看起来像这样?
07.12
Run Code Online (Sandbox Code Playgroud) 当你写int i; cin >> i;输入00325或325,两者的投入表现得像i = 325;.但为什么?我认为如果你输入i的价值设定为更自然,那么下一个产生了,而下一个产生了.我知道可以是八进制数,但我现在输入数字作为十进制数(我不使用操纵器).000325cin >>i;i = 0;cin >> i;i = 325;00325oct
什么书面证据保证了这种行为?我快速浏览了N4140,却找不到任何证据.
注意:我想要的是不知道在输出时保持前面的零的方法,这在使用cout << operator时如何用前导零填充int中讨论?.
一旦有人给我这样的句子,我的问题就会得到解决
preceding zeros are ignored when setting a number-type object as the right operand of <<
在一些可靠的文件中.
我想打印出一个类似于minutes:second.miliseconds的时间。想想那个时间是4分钟2秒65毫秒,
而不是像4:2.65那样写,我想像4:02.065那样写?