C++语法基础--ostream,cout及其格式控制,缓冲区

布满荆棘的人生 2021-11-11 02:40 1018阅读 0赞

**1.C++程序把输入和输出看作字节流:输入时,程序从输入流中抽取字节;输出是,程序将字节插入到输出流中。流充当了程序和流源或流目标之间的桥梁。

2.缓冲区是用作中介的内存块,它将信息从设备传输到程序或从程序传输给设备的临时存储工具。

3.在程序中包含iostream文件将自动创建8个流对象(4个窄字符流,4个宽字符流)
cin对象对应于标准输入流 wcin与cin类似,但处理的是wchar_t类型(下同)
cout对象对应于标准输出流 ,wcou类似。
cerr对象对应于标准错误流,这个流没有被缓冲,wcerr类似
clog对象也对应着标准错误流,这个流被缓冲,wclog类似

4.重定向
标准输入和输出流通常连接着键盘和屏幕。
<:输入重定向

:输出重定向

5.使用cout进行输出
ostream将数据内部表示(二进制位模式)转换为由字符组成的输出流
*ostream类重载了<<运算符,使其能识别C++中所有的基本类型
在C++中<<运算符的默认含义是按位左移**

Example:

x<<3表示将x的二进制表示中所有的位向左移动3位

*输出和指针
ostream类还定义了下面指针类型的插入运算符函数
const signed char*;
const unsigned char*;
const char*;
void*;
C++用指向字符串存储位置的指针来表示字符串,
char ch[]=”hello”;
char *p=”hello”;

因此:

cout<<ch; cout<<p;

都将输出字符串

*\对于其它类型的指针,C++将其对应与void*,并打印其地址的数值表示,如果要输出字符串地址,则必须将其 转换为其他类型
Example:
char *p=hello;
cout<<(void*)p;
*ostream类的put(),write方法
ostream& put (char c):
Put character
Inserts character c into the stream.

ostream& write (const char* s, streamsize n):
Write block of data
Inserts the first n characters of the array pointed by s into the stream.
Tips:
write()方法并不会在遇到空白字符时自动停止打印字符,而只是打印指定数目字符,即使字符超出了边界,
write不会将数字转换为相应的字符,而是传输内存中存储的位表示,但输出设备将把每个字节作为ASCll码解释
Example:
int main()
{

const char* c=”hello”;
const char* c1=”nihao”;
cout.put(‘H’).put(‘i’)<<endl;
cout.write(c,7)<<endl;
int a=97; //97的ASCll码为a
cout.write((char*)&a,sizeof(int));
return 0;
}

output:
Hi
hello n
a

6.刷新输出缓冲区
通常缓冲区为512字节或其整数倍,当缓冲区填满时,程序将刷新缓冲区,把内容发送出去,并清空缓冲区
*强行进行刷新:
flush:刷新缓冲区
endl:刷新缓冲区并插入换行符
Example:
int main()
{

cout<<”hello”<<flush;
cout<<” nihao”<<endl;
return 0;
}

outpu:
hello nihao**

*事实上控制符也是函数,

例如

flush(cout);

*7.用cout进行格式化
\
默认情况下,每个值都等于它的长度
由于ios_base->ios->ostream,因此ios的格式控制符可以用于ostream对象或其子代,如cout
ios_base& hex (ios_base& str):
Use hexadecimal base
Sets the basefield format flag for the str stream to hex.

ios_base& oct (ios_base& str):
Use octal base
Sets the basefield format flag for the str stream to oct.

ios_base& dec (ios_base& str):
Use decimal base
Sets the basefield format flag for the str stream to dec.

Tips:
一旦设置了格式控制符,将一直持续到下一个格式控制符出现为止
Example:
int main()
{

int a=13;int b=14;
cout<<hex<<a<<endl; //d
cout<<b<<endl; //e
cout<<oct<<a<<endl; //15
cout<<b<<endl; //16
cout<<dec<<a<<endl; //13
cout<<b<<endl; //14

return 0;
}

8.调整字段宽度
streamsize width() const:
Get field width,returns the current value of the field width.

streamsize width (streamsize wide);
get field width,also sets a new field width for the stream.
Tips:
width方法只影响接下来显式的一个项目,然后恢复默认值,width默认情况下为右对齐
Example:
int main()
{

int size=cout.width(20);
cout<<”size is: “<<size<<endl;
cout<<”hello”<<endl;

return 0;
}

运行结果:
Center

9.填充字符
*新的填充字符将一直有效,直到更改它为止
char fill() const;
Get fill character, returns the fill character.

char fill (char fillch);
sets fillch as the new fill character and returns the fill character used before the call.

Example:
int main()
{
cout.fill(‘*‘);
cout.width(30);
cout<<”hello”<<endl;
char c=cout.fill();
cout<<c<<endl;

return 0;
}**

运行结果:

Center 1

**10.设置浮点数的显式精度
C++的默认精度为6位(但末尾的0将不显示)
streamsize precision() const;
Set floating-point decimal precision
returns the value of the current floating-point precision field for the stream.

streamsize precision (streamsize prec);
Set floating-point decimal precision
returns the value of the current floating-point precision field for the stream, also sets it to a new valu

Example:
int main()
{
int n=cout.precision();
cout<<n<<endl;
cout<<(float)1/3<<endl;
cout.precision(10);
cout<<(float)1/3<<endl;

return 0;
}

运行结果:
Center 2**

发表评论

表情:
评论列表 (有 0 条评论,1018人围观)

还没有评论,来说两句吧...

相关阅读

    相关 objective C基础语法

    最近工作需要用到oc,紧急学了一下。 oc是iOS开发使用到的语言,是c的超集,是一种面向对象语言。 一、文件扩展名 .h 头文件,包含类,类型,函数和常数的声明。

    相关 C#语法基础

            C\语言,跟之前我们学习过的Vb,Java语言类似,都是一种计算机编程语言。       学习一门语言之前,必不可少的就是要了解这门语言的语法知识,而各语