由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++ formatted output question
相关主题
问个指针array 的简单问题菜鸟请教smart pointer
C++格式输出在帮忙看看这个吧 C: int->char*
a C++ interview question..........array和pointer在作为函数返回时有啥区别 (C)
请教 C++的一个困惑 (operator delete)C 语言,初学者,简单问题
问题很不习惯cin/cout
最新某公司onsite面试题 (转载)C#的formated output有点不方便呀。。。。
RH3/RH4上cout默认格式问题谁来解释一下这个是compiler问题吗?
[合集] question about a static pointer in a member functionc++ 中如何把str转换为float?
相关话题的讨论汇总
话题: cout话题: output话题: c++话题: formatted
进入Programming版参与讨论
1 (共1页)
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.
1 (共1页)
进入Programming版参与讨论
相关主题
c++ 中如何把str转换为float?问题
关于数组最新某公司onsite面试题 (转载)
C++里get array size的问题 (转载)RH3/RH4上cout默认格式问题
c++ pointer conversion question[合集] question about a static pointer in a member function
问个指针array 的简单问题菜鸟请教smart pointer
C++格式输出在帮忙看看这个吧 C: int->char*
a C++ interview question..........array和pointer在作为函数返回时有啥区别 (C)
请教 C++的一个困惑 (operator delete)C 语言,初学者,简单问题
相关话题的讨论汇总
话题: cout话题: output话题: c++话题: formatted