COUT的使用
来源:互联网 发布:上传身份证照片js插件 编辑:程序博客网 时间:2024/06/01 21:26
Standard C++ Library
Copyright 1998, Rogue Wave Software, Inc.
NAME
cout
- Controls output to a stream buffer associated with the object
stdout declared in <cstdio>.
SYNOPSIS
#include <iostream>
extern ostream cout;
ostream cout;
DESCRIPTION
The object cout controls output to a stream buffer associated with the
object stdout declared in <cstdio>. By default the standard C and C++
streams are synchronized, but performance improvement can be achieved
by using the ios_base member function synch_with_stdio to
desynchronize them.
After the object cin is initialized, cin.tie() returns &cout, which
implies that cin and cout are synchronized.
FORMATTING
The formatting is done through member functions or manipulators.
MANIPULATORS MEMBER FUNCTIONS
showpos setf(ios_base::showpos)
noshowpos unsetf(ios_base::showpos)
showbase setf(ios_base::showbase)
- 1 - Formatted: June 20, 2006
cout(3C++) Rogue Wave Software cout(3C++)
- -
02 Apr 1998
noshowbase unsetf(ios_base::showbase)
uppercase setf(ios_base::uppercase)
nouppercase unsetf(ios_base::uppercase)
showpoint setf(ios_base::showpoint)
noshowpoint unsetf(ios_base::showpoint)
boolalpha setf(ios_base::boolalpha)
noboolalpha unsetf(ios_base::boolalpha)
unitbuf setf(ios_base::unitbuf)
nounitbuf unsetf(ios_base::unitbuf)
internal setf(ios_base::internal,
ios_base::adjustfield)
left setf(ios_base::left,
ios_base::adjustfield)
right setf(ios_base::right,
ios_base::adjustfield)
- 2 - Formatted: June 20, 2006
cout(3C++) Rogue Wave Software cout(3C++)
- -
02 Apr 1998
dec setf(ios_base::dec,
ios_base::basefield)
hex setf(ios_base::hex,
ios_base::basefield)
oct setf(ios_base::oct,
ios_base::basefield)
fixed setf(ios_base::fixed,
ios_base::floatfield)
scientific setf(ios_base::scientific,
ios_base::floatfield)
resetiosflags
(ios_base::fmtflags flag) setf(0,flag)
setiosflags
(ios_base::fmtflags flag) setf(flag)
setbase(int base) see above
setfill(char_type c) fill(c)
setprecision(int n) precision(n)
setw(int n) width(n)
- 3 - Formatted: June 20, 2006
cout(3C++) Rogue Wave Software cout(3C++)
- -
02 Apr 1998
endl
ends
flush flush( )
DESCRIPTION
showpos Generates a + sign in non-negative generated numeric output.
showbase Generates a prefix indicating the numeric base of generated
integer output
uppercase Replaces certain lowercase letters with their uppercase
equivalents in generated output
showpoint Generates a decimal-point character unconditionally in
generated floating-point output
boolalpha Inserts and extracts bool type in alphabetic format
unitbuf Flushes output after each output operation
internal Adds fill characters at a designated internal point in
certain generated output. If no such point is designated,
it's identical to right.
left Adds fill characters on the right (final positions) of certain
generated output
right Adds fill characters on the left (initial positions) of
certain generated output
- 4 - Formatted: June 20, 2006
cout(3C++) Rogue Wave Software cout(3C++)
- -
02 Apr 1998
dec Converts integer input or generates integer output in decimal
base
hex Converts integer input or generates integer output in
hexadecimal base
oct Converts integer input or generates integer output in octal base
fixed Generates floating-point output in fixed-point notation
scientific Generates floating-point output in scientific notation
resetiosflags
(ios_base::fmtflags flag) Resets the fmtflags field flag
setiosflags
(ios_base::fmtflags flag) Sets up the flag flag
setbase(int base) Converts integer input or generates integer output
in base base. The parameter base can be 8, 10 or
16.
setfill(char_type c) Sets the character used to pad (fill) an output
conversion to the specified field width
setprecision(int n) Sets the precision (number of digits after the
decimal point) to generate on certain output
conversions
- 5 - Formatted: June 20, 2006
cout(3C++) Rogue Wave Software cout(3C++)
- -
02 Apr 1998
setw(int n) Sets the field with (number of characters) to generate
on certain output conversions
showpos Generates a + sign in non-negative generated numeric output.
showbase Generates a prefix indicating the numeric base of generated
integer output
uppercase Replaces certain lowercase letters with their uppercase
equivalents in generated output
showpoint Generates a decimal-point character unconditionally in
generated floating-point output
boolalpha Inserts and extracts bool type in alphabetic format
unitbuf Flushes output after each output operation
internal Adds fill characters at a designated internal point in
certain generated output. If no such point is designated,
it's identical to right.
left Adds fill characters on the right (final positions) of certain
generated output
right Adds fill characters on the left (initial positions) of
certain generated output
- 6 - Formatted: June 20, 2006
cout(3C++) Rogue Wave Software cout(3C++)
- -
02 Apr 1998
dec Converts integer input or generates integer output in decimal
base
hex Converts integer input or generates integer output in
hexadecimal base
oct Converts integer input or generates integer output in octal base
fixed Generates floating-point output in fixed-point notation
scientific Generates floating-point output in scientific notation
resetiosflags
(ios_base::fmtflags flag) Resets the fmtflags field flag
setiosflags
(ios_base::fmtflags flag) Sets up the flag flag
setbase(int base) Converts integer input or generates integer output
in base base. The parameter base can be 8, 10 or
16.
setfill(char_type c) Sets the character used to pad (fill) an output
conversion to the specified field width
setprecision(int n) Sets the precision (number of digits after the
decimal point) to generate on certain output
conversions
- 7 - Formatted: June 20, 2006
cout(3C++) Rogue Wave Software cout(3C++)
- -
02 Apr 1998
setw(int n) Sets the field with (number of characters) to generate
on certain output conversions
endl Inserts a newline character into the output sequence and flush
the output buffer.
ends Inserts a null character into the output sequence.
flush Flush the output buffer.
DEFAULT VALUES
precision() 6
width() 0
fill() the space character
flags() skipws | dec
getloc() locale::locale()
EXAMPLE
//
// cout example #1
//
#include<iostream>
#include<iomanip>
void main ( )
{
using namespace std;
int i;
float f;
// read an integer and a float from stdin
cin >> i >> f;
// output the integer and goes at the line
cout << i << endl;
// output the float and goes at the line
- 8 - Formatted: June 20, 2006
cout(3C++) Rogue Wave Software cout(3C++)
- -
02 Apr 1998
cout << f << endl;
// output i in hexa
cout << hex << i << endl;
// output i in octal and then in decimal
cout << oct << i << dec << i << endl;
// output i preceded by its sign
cout << showpos << i << endl;
// output i in hexa
cout << setbase(16) << i << endl;
// output i in dec and pad to the left with character
// @ until a width of 20
// if you input 45 it outputs 45@@@@@@@@@@@@@@@@@@
cout << setfill('@') << setw(20) << left << dec << i;
cout << endl;
// output the same result as the code just above
// but uses member functions rather than manipulators
cout.fill('@');
cout.width(20);
cout.setf(ios_base::left, ios_base::adjustfield);
cout.setf(ios_base::dec, ios_base::basefield);
cout << i << endl;
// outputs f in scientific notation with
// a precision of 10 digits
cout << scientific << setprecision(10) << f << endl;
// change the precision to 6 digits
// equivalents to cout << setprecision(6);
cout.precision(6);
// output f and goes back to fixed notation
cout << f << fixed << endl;
}
//
// cout example #2
//
#include <iostream>
void main ( )
{
using namespace std;
- 9 - Formatted: June 20, 2006
cout(3C++) Rogue Wave Software cout(3C++)
- -
02 Apr 1998
char p[50];
cin.getline(p,50);
cout << p;
}
//
// cout example #3
//
#include <iostream>
#include <fstream>
void main ( )
{
using namespace std;
// open the file "file_name.txt"
// for reading
ifstream in("file_name.txt");
// output the all file to stdout
if ( in )
cout << in.rdbuf();
else
{
cout << "Error while opening the file";
cout << endl;
}
}
WARNINGS
Keep in mind that the manipulator endl flushes the stream buffer.
Therefore it is recommended to use `/n' if your only intent is to go
at the line. It greatly improves performance when C and C++ streams
are not synchronized.
If your compiler does not support namespaces, then you do not need the
using declaration for std.
SEE ALSO
basic_ostream(3C++), basic_iostream(3C++), basic_filebuf(3C++),
cin(3C++), cerr(3C++), clog(3C++), wcin(3C++), wcout(3C++),
wcerr(3C++), wclog(3C++), iomanip(3C++)
- COUT的使用
- Cout.width()的使用
- cout.precision()的使用
- cout格式输出的使用
- cout<<格式输出的使用
- cin和cout的使用
- g++std::cout的使用
- Qt中使用cout输出的方法
- printf与cout同时使用的问题
- 使用cout输出控制小数位的方法
- 关于C++中cout的使用
- 使用cout进行输出
- 控制台console使用MFC库函数,Cout输出CString的方法
- <C/C++> IO流 cout/fstream的使用
- 关于Qt无法直接使用cout和cin的问题
- 解决vs2010中C++的cout使用问题
- 使用cout输出小数时位数的控制方法
- 关于Qt无法直接使用cout和cin的问题
- PHP中的ob_start()
- PHP字符串函数与使用分析
- XMLTextReader和XmlDocument读取XML文件的比较
- ASP.NET常用的三十三种实用代码
- smart form 例子
- COUT的使用
- PHP批量生成缩略图
- 用封装类来合理的设计PHP项目--谈PHP项目中类的封装
- 重生(2005年9月写的,现在我已经有了比文章描述的更成熟的思想,欢迎大家指正)
- (突然想到的测试方法1)把基础测试叠加,在加上方法,也许能测是全面的-2
- php header & http
- Mac OS X 环境变量设置
- 有了Web Service,还需要.NET吗?
- 染色体、基因与人体基因组研究(转载于新华网)