缓冲是指在内存里有一片缓冲区域,将输出的内容先缓存到缓冲区域,等到缓冲区满或者手动刷新缓冲区时,再写到硬盘上.
| 对象 | 含义 | 对应设备 | c语言中相应的标准文件 |
|---|---|---|---|
| cin | 标准输入流 | 键盘 | stdin |
| cout | 标准输出流 | 屏幕 | stdout |
| cerr | 标准错误流 | 屏幕 | stderr |
| clog | 标准错误流 | 屏幕 | stderr |
cout是console output的缩写,cout不是C++预定义的关键字,它是ostream流类的对象,在iostream中定义。
cerr,clog流对象是标准错误流.二者的区别是cerr是不经过缓冲区,直接向显示器上输出有关信息,而clog中的信息存放在缓冲区中,缓冲区满后或遇endl时向显示器输出。
标准输入函数
标准输入附属到标准输入设备,通常是键盘
| 函数 | 说明 |
|---|---|
| cin.get() | |
| cin.getline() | |
| cin.ignore() | |
| cin.peek() | |
| cin.putback() |
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
string str = "Hello C++";
cout << str << endl;
system("pause");
return EXIT_SUCCESS;
}
标准输出函数
| 函数 | 描述 |
|---|---|
| cout.flush() | 刷新缓存区(linux) |
| cout.put() | 向缓存区写字符 |
| cout.write() |
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
string str;
cout << "请输入您的名称: ";
cin >> str;
cout << "您的名称是: " << str << endl;
system("pause");
return EXIT_SUCCESS;
}
标准错误流
cerr一般附属到错误设备(也是显示屏),但是cerr是非缓冲的,每个流到cerr都会立刻输出
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
string str = "Unable to read....";
cerr << "Error message : " << str << endl;
system("pause");
return EXIT_SUCCESS;
}
标准日志流
clog附属到标准错误设备(显示屏),但是clog对象是缓冲的,只有缓冲区满或者刷新时才会输出.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
string str = "Unable to read....";
clog << "Error message : " << str << endl;
system("pause");
return EXIT_SUCCESS;
}
cerr流来显示错误信息,clog来打印日志
格式化输出
操纵符(manipulator)
格式化布尔值boolalpha
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
cout << true << endl;
cout << boolalpha << true << endl;
system("pause");
return EXIT_SUCCESS;
}
<iostream>中操纵符
| 操纵符 | 描述 |
|---|---|
| boolalpha | 将true和false显示为字符串,默认为noboolalpha |
| showbase | 对整型值输出表示进制的前缀,默认为noshowbase |
| showpoint | 显示小数点,noshowpoint |
| showpos | 对非负数显示+号,默认为noshowpos |
| uppercase | 十六进制中打印0X,科学计数法中打印E,nouppercase为小写0x和e |
| dec | 十进制 |
| hex | 十六进制 |
| oct | 八进制 |
| left | 左对齐 |
| right | 右对齐 |
| internal | 符号和值之间填充字符 |
| fixed | 浮点值显示为十进制 |
| scientific | 浮点值显示为科学计数法 |
| hexfloat | 浮点值显示为十六进制 |
| hexfloat | 浮点值显示为八进制 |
| defaultfloat | 重置浮点值为十进制 |
| unitbuf | 每次操作后都刷新缓冲区,默认nounitbuf |
| skipws | 输入运算符跳过空白符,noskipws |
| flush | 刷新缓冲区 |
| ends | 插入空字符,然后刷新缓冲区 |
| endl | 插入换行,然后刷新缓冲区 |
<iomanip>中的操纵符
| 操纵符 | 描述 |
|---|---|
| setfill(ch) | 用ch填充空白 |
| setprecision(n) | 设置浮点精度 |
| setw(w) | 设置宽度 |
| setbase(b) | 将整数输出为二进制 |
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
int main(int argc, char** argv)
{
cout << sqrt(200) << endl;//默认状态
cout << setprecision(2) << sqrt(200) << endl;//设置精度
cout << scientific << sqrt(200) << endl;//科学计数
system("pause");
return EXIT_SUCCESS;
}
格式成员函数
| 函数1 | 函数2 | 描述 |
|---|---|---|
| precision(n) | setprecision(n) | 设置精度 |
| width(n) | setw(n) | 设置字段宽 |
| fill© | setfill© | 设置填充字符 |
| setf() | setiosflags() | 设置输出格式状态 |
| unsetf() | resetioflags() | 终止格式状态 |
434




被折叠的 条评论
为什么被折叠?



