s******s 发帖数: 505 | 1 How to output in pointer format with cout?
for example, in C, printf("%p",charp);
Thank you. |
t****t 发帖数: 6806 | 2 %p basically means %#x. so it's just
cout<<"0x"< |
t****t 发帖数: 6806 | 3 let me correct myself -- actually, when you insert a pointer, the format
specifier is automatically %p. so just
cout<
is enough.
【在 t****t 的大作中提到】 : %p basically means %#x. so it's just : cout<<"0x"<
|
s******s 发帖数: 505 | 4
1 #include
2
3 int main() {
4 char* cp;
5 cp = new char('a');
6 std::cout << cp << " . " << *cp << std::endl;
7 }
$ ./a.out
a . a
【在 t****t 的大作中提到】 : let me correct myself -- actually, when you insert a pointer, the format : specifier is automatically %p. so just : cout<: is enough.
|
t****t 发帖数: 6806 | 5 char* or const char* are special so that they are output as string. cast it
to (void*).
【在 s******s 的大作中提到】 : : 1 #include : 2 : 3 int main() { : 4 char* cp; : 5 cp = new char('a'); : 6 std::cout << cp << " . " << *cp << std::endl; : 7 } : $ ./a.out : a . a
|
s******s 发帖数: 505 | 6
it
That works! Thank you!
【在 t****t 的大作中提到】 : char* or const char* are special so that they are output as string. cast it : to (void*).
|
s******s 发帖数: 505 | 7 another question,
y = 6.0;
cout << y << endl;
will output '6'.
How to make it output '6.00'? |
f******y 发帖数: 2971 | 8 std::setprecision(2)
【在 s******s 的大作中提到】 : another question, : y = 6.0; : cout << y << endl; : will output '6'. : How to make it output '6.00'?
|
s******s 发帖数: 505 | 9
setprecision(2) makes 6.666 to 6.66, but not 6 to 6.00
【在 f******y 的大作中提到】 : std::setprecision(2)
|
t****t 发帖数: 6806 | 10 (in addition to setprecision)
cout<
or
cout.fixed();
to cancel, use
str.setf(~ios_base::floatfield, ios_base::floatfield);
【在 s******s 的大作中提到】 : : setprecision(2) makes 6.666 to 6.66, but not 6 to 6.00
|
s******s 发帖数: 505 | 11
Thank you, by the way, is there a way to print a boolean
variable as 'true/false' instead of '0/1' by setting flags
or using manipulators?
【在 t****t 的大作中提到】 : (in addition to setprecision) : cout<: or : cout.fixed(); : to cancel, use : str.setf(~ios_base::floatfield, ios_base::floatfield);
|
t****t 发帖数: 6806 | 12 cout<
cout<
go buy a reference book.
【在 s******s 的大作中提到】 : : Thank you, by the way, is there a way to print a boolean : variable as 'true/false' instead of '0/1' by setting flags : or using manipulators?
|
s******s 发帖数: 505 | 13
blush; thank you.
【在 t****t 的大作中提到】 : cout<: cout<: go buy a reference book.
|