第二十一章流 1流的操作 简单

// 第二十一章流 1流的操作
// 1.1 缓冲
// 1.2 流和缓冲区
// 1.3 标准输入输出对像
// 1.4 重定向 重定向的意思是可以重新定向输出的设备
// 1.5 对像代表流
// 2.0 用count输出
/*#include <iostream>
using namespace std;
int main()
{
	char *ch = "hello world";
	//用强制转换方式将char*指针ch转换为void*之后,输出的就是字符串ch的地址,也就是第一个字符的位置
	cout<<(void*)ch<<endl;
	//输出的&ch则是指针ch的地址
	cout<<&ch<<endl;
}*/
// 2.2 清理缓冲区
// 2.3 有关输出的相关函数
   //ostring类除了提供各种类型的operator<<()函数外,还提供了put()和write()函数
   //put函数用于输出单个字符 write()函数用于输出字符
/*#include <iostream>
using namespace std;
int main()
{
	cout.put('h').put('e').put('l').put('p').put('\n');
	cout<<endl;
	char ch[] = "hello world";
	int length = strlen(ch); 
	cout.write(ch,length).put('\n').write(ch,length-6)<<endl;

    return 0;
}*/
//2.4 设置输出的字段宽度
/*
#include <iostream>
using namespace std;
int main()
{
	cout.width(20);//width()只影响将要显示的对像,然后字段宽度自动恢复为默认值
	cout<<"help"<<endl;
	cout<<"help\n";
	cout.width(1);
	cout<<"help"<<endl;
    return 0;
}*/
// 2.5 设置填充字段
/*#include <iostream>
using namespace std;
int main()
{
	cout.width(20);
	cout.fill('*'); //调用fill()方法来填充为其它字符合
	cout<<"help"<<endl;
	cout.fill(' ');
	cout.width(25);
	cout<<"help"<<endl;
    return 0;
}*/

// 2.6 设置浮点数的显示精度
/*#include <iostream>
using namespace std;
int main()
{
	//浮点数据默认精度为6到7位
    float pi = 3.1415926f;
	float p2 = 9876.622345f;
	cout<<pi<<endl;
	cout<<p2<<endl;
	cout<<"设置显示精度为15位"<<endl;
	cout.precision(15);
	cout<<pi<<endl;
	cout<<p2<<endl;
	cout<<"设置显示精度为3位"<<endl;
	cout.precision(3);
	cout<<pi<<endl;
	cout<<p2<<endl;
	return 0;
}*/

// 2.7 输出末尾的0_setf()
/*#include <iostream>
using namespace std;
int main()
{
	float price = 4.0f;
	cout.precision(6);
	cout.setf(ios_base::showpoint);//showpints指示是否显示点九,true为显示 false为不显示
	cout<<price<<endl;
	return 0;
}*/

// 2.8 设置标志
//cout.setf(ios::left);    按指定的宽度向左对齐
//cout.setf(ios::internal);符号左对齐,值右对齐
//count.setf(ios::right);  按指定的宽度向右对齐全
/*#include <iostream>
using namespace std;
int main()
{
	const float number = -185;
	cout.width(10);
	cout.setf(ios::internal);
	cout<<number<<endl;

	cout<<"计算15个标志的十进制值:"<<endl;
	cout<<"boolalpha:"<<ios::boolalpha<<endl;
	cout<<"fixed:"<<ios::fixed<<endl;
	cout<<"scientific:"<<ios::scientific<<endl;
	cout<<"hex:"<<ios::hex<<endl;
	cout<<"oct:"<<ios::oct<<endl;
	cout<<"dec:"<<ios::dec<<endl;
	cout<<"internal:"<<ios::internal<<endl;
	cout<<"right:"<<ios::right<<endl;
	cout<<"left:"<<ios::left<<endl;
	cout<<"showpos:"<<ios::showpos<<endl;
	cout<<"showpoint:"<<ios::showpoint<<endl;
	cout<<"showbase:"<<ios::showbase<<endl;
	cout<<"uppercase:"<<ios::uppercase<<endl;
	cout<<"unitbuf:"<<ios::unitbuf<<endl;
	//cout<<"skipws"<ios::skipws<<endl; 这个不知道为什么并没有开启
	cout<<endl;
    return 0;
}*/
/*
#include <iostream>
using namespace std;
int main()
{
   char ch[10];
   cin>>ch;
   cout<<ch;
   return 0;
}*/
/*
//setf()函数的返回值
#include <iostream>
using namespace std;
int main()
{
	int x;
	x = cout.setf(ios::left);
	cout<<x<<endl;
	x = cout.setf(ios::right);
	cout<<x<<endl;
	x = cout.setf(ios::internal);
	cout<<x<<endl;
	system("pause");
    return 0;
}*/
//标志设置不当出现的错误
/*#include <iostream>
using namespace std;
int main()
{
	const float number = -185;
	cout.width(10);
	cout.setf(ios::left);
	cout<<number<<endl;
	cout.width(10);
	cout.setf(ios::right);
	cout<<number<<endl;
	cout.width(10);
	cout.setf(ios::internal);
	cout<<number<<endl;
    return 0;
}*/

// 2.9 setf()函数原型
// 1 fmtflags setf(fmtflags)
// 2 fmtflags setf(fmtflags, fmtflags) 第二个参数则指定要清除的标志位,来统一管理这三个标志,这个常叫指示标志位,它的作用是恢复三个标志left,right internal为初始化状态
//ios::adjustfield
/*#include <iostream>
using namespace std;
int main()
{
	const float number = -185;
	cout.width(10);
	cout.setf(ios::left);
	cout<<number<<endl;
	cout.width(10);
	cout.setf(ios::right,ios::adjustfield);
	cout<<number<<endl;
	cout.width(10);
	cout.setf(ios::internal,ios::adjustfield);
	cout<<number<<endl;
    return 0;
}*/


// 2.10 所有15个标志以及3个指示标志
/*#include <iostream>
using namespace std;
int main()
{
	int x=185;
	cout.setf(ios::showpos);
	cout<<x<<endl;
	float y = 193.34f;
	cout.setf(ios::scientific);
	cout<<y<<endl;
	cout.unsetf(ios::showpos);
	cout<<x<<endl;
	cout.unsetf(ios::scientific);
	cout<<y<<endl;
    return 0;
}*/

// 2.11 unset()函数 可以实现标记位的清空,该函数的使用方法如下
/*
#include <iostream>
using namespace std;
int main()
{
	const float number = -185;
	cout.width(10);
	cout.setf(ios::left);
	cout<<number<<endl;
	cout.width(10);
	cout.unsetf(ios::adjustfield);
	cout.setf(ios::right);
	cout<<number<<endl;
	cout.width(10);
	cout.unsetf(ios::adjustfield);
	cout.setf(ios::internal);
	cout<<number<<endl;
    return 0;
}*/


// 2.12 标准控制器
// 标准控制器的使用
/*
#include <iostream>
using namespace std;
int main()
{
	const int x = 703710; 
	cout<<"x:"<<oct<<x<<endl; //八进制输出
	cout<<"x:"<<showbase<<x<<endl; //在八进制前添加0
	cout<<"x:"<<hex<<x<<endl;      //十六进制输出
	cout<<"x:"<<uppercase<<x<<endl;//用大写表示十六进制
	cout<<"x:"<<dec<<x<<endl;      //十进制输出
	cout<<"x:"<<showpos<<x<<endl;  //在正数前添车+号

	float y=3.56000f;
	cout.precision(0); //设置精度为0;
    cout<<"y:"<<scientific<<y<<endl; //以科学计数法输出浮点数
	cout.unsetf(ios::floatfield);
	cout<<"y:"<<showpoint<<y<<endl;
	//精度为0时输出浮点数的有效位数字(6或7位),精度不为0时输出该精度限制内的数字,这个精度指的是总精度,这两种情况均包括小数点
	cout.unsetf(ios::showpoint); //去掉小数点显示
	cout<<"y:"<<fixed<<y<<endl; //精度指的是小数点后的精度,也不包括小数点
	char str1[] = "abc";
	char str2[] = "abc";
	const char str3[] = "abc";
	const char str4[] = "abc";
	const char* str5 = "abc";
	const char* str6 = "abc";
	cout<<boolalpha<<(str1==str2) <<endl;
	//str1和str2都是字符数组,每个都有其自己的存储区,它们的值则是各存储区首地址,因此不相等

	cout<<boolalpha<<(str3==str4)<<endl;
	//str3和str4同上,只不过多了个const表示它们所指向的数据区不能修改因此不相

	cout<<boolalpha<<(str5==str6)<<endl;
	//str5和str6并非数组而是字符指针,并不分配存储区,str5和str6中的"abc"以常量形式存于存储取
	//也就是静态数据区,而它们自己仅是指向该区首地址的指针相
	//分别输出false false true

	cout.width(20);
	cout<<right<<y<<endl; 
	cout.width(20);
	cout<<left<<y<<endl;
	cout.width(20);
	cout<<internal<<y<<endl;

    return 0;
}*/

//2.13 iomanip头文件与标准控制符
//最常风的控制符分别是 setw() setfile() setprecision()0
/*#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	float n=3.1456f;
	for(int i=0; i<5; i++)
	{
	    n = n*n;
		cout<<setw(6)<<setfill('.')<<i<<setfill(' ')<<setw(22)<<setprecision(2)<<fixed<<n<<endl;
	}
    return 0;
}*/

//showpoint标志的作用
/*
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	float n=3.1456f;
	for(int i=0; i<5; i++)
	{
	    n = n*n;
		cout<<setw(6)<<setfill('.')<<i<<setfill(' ')<<setw(22)<<setprecision(2)<<showpoint<<n<<endl;
	}
    return 0;
}
*/

  

原文地址:https://www.cnblogs.com/xiangxiaodong/p/2709693.html