网站 php .net,专业的推广公司,推广引流吸引人的文案,信息网站方案目录1、格式化输出1. setw manipulator(“设置域宽”控制符)2. setprecision manipulator(“设置浮点精度”控制符)3. setfill manipulator(“设置填充字符”控制符)4. Formatting Output in File Operation(在文件操作中格式化输入/输出)5.小练习2、用于输入/输出流的函数1. g… 目录1、格式化输出1. setw manipulator(“设置域宽”控制符)2. setprecision manipulator(“设置浮点精度”控制符)3. setfill manipulator(“设置填充字符”控制符)4. Formatting Output in File Operation(在文件操作中格式化输入/输出)5.小练习2、用于输入/输出流的函数1. getline()2. get() and put()3. flush()4.getline()练习 1、格式化输出
1. setw manipulator(“设置域宽”控制符)
要包含头文件 setw(n) 设置域宽即数据所占的总字符数
std::cout std::setw(3) a std::endl;
输出
_ _asetw()控制符只对其后输出的第一个数据有效
std::cout std::setw(5) a b std::endl;
输出
_ _ _ _absetw()的默认为setw(0)按实际输出。 如果输出的数值占用的宽度超过setw(int n)设置的宽度则按实际宽度输出。
float f0.12345;std::cout std::setw(3) f std::endl;
输出
0.123452. setprecision manipulator(“设置浮点精度”控制符)
setprecision(int n) (1) 控制显示浮点数的有效位 (2) n代表数字总位数不包括小数点 #include iostream
#include iomanip
using namespace std;int main() {float f 17 / 7.0;cout f endl;cout setprecision(0) f endl;cout setprecision(1) f endl;cout setprecision(2) f endl;cout setprecision(3) f endl;cout setprecision(6) f endl;cout setprecision(8) f endl;return 0;}VS效果 2.42857 2.42857 2 2.4 2.43 2.42857 2.4285715 3. setfill manipulator(“设置填充字符”控制符)
setfill© 设置填充字符即“符号后面的数据长度小于域宽时使用什么字符进行填充。
std::cout std::setfill(*) std::setw(5) a std::endl;
输出
****a4. Formatting Output in File Operation(在文件操作中格式化输入/输出) 5.小练习 本部分展示内容如下; 任务1展示setw和setfill 1、setw只对紧跟随其后的数据起作用 2、setfill指定填充字符 任务2展示setprecision、fixed、showpoint、left、right 任务3展示hexfloat #include iostream
#include iomanipusing std::cout;
using std::endl;
int main()
{//任务1展示setw和setfill//cout std::setw(4) std::setfill(#) a;cout std::setfill(#);for (int i 0;i 5;i){cout std::setw(i2) endl;}//任务2展示setprecision、fixed、showpoint、left、rightdouble pi 3.1415926535897;cout std::setprecision(6) pi endl;//定点数代表了小数点后几位cout std::setprecision(6) std::fixed pi endl;double y 3.0;cout y endl;cout std::showpoint y endl;cout std::setw(20) std::left pi endl;cout std::setw(20) std::right pi endl;//任务3展示hexfloatcout std::hexfloat y endl;cout std::defaultfloat;cout y endl;cout std::showpoint y endl;return 0;
}2、用于输入/输出流的函数
1. getline() 运算符用空格分隔数据 对于文件内容 Li Lei#Han Meimei#Adam 如下代码只能读入“Li”
ifstream input(name.txt);
std::string name;
input name;如果用成员函数getline(char* buf, int size, char delimiter)读LiLei
constexpr int SIZE{ 40 };
std::arraychar , SIZE name{};
while (!input.eof()) {// not end of fileinput.getline(name[ 0 ] , SIZE , #);std::cout name[ 0 ] std::endl;
}如果用非成员函数getline(istream is, string str, char delimiter)读LiLei
std::string name2{};
while (!input.eof()) {std::getline(input, name2, #);std::cout n std::endl;
}2. get() and put()
get: read a character
//这一种需要将int类型强制转换为char类型
//char c static_castchar(in.get());
int istream::get();
//char c; in.get(c);
istream get (char c);put write a character
ostream put (char c);3. flush()
将输出流缓存中的数据写入目标文件
ostream flush();用法
cout.flush(); // 其它输出流对象也可以调用 flush()
cout Hello std::flush; // 与endl类似作为manipulator的调用方式4.getline()练习 本部分要展示的内容如下; 任务1展示istream::getline函数的用法 任务2展示std::getline函数的用法 #include iostream
#include fstream
#include array
#include string
#include filesystem
using std::cout;
using std::endl;
using std::ifstream;
using std::string;int main()
{//打开文件std::filesystem::path p{ scores.txt };ifstream in{p};if (!in){cout Cant open file p endl;std::abort();}//任务1istream::getline函数constexpr int SIZE 1024;std::arraychar, SIZE buf; //bufwhile (!in.eof()){in.getline(buf[0], SIZE, #);cout buf[0] endl;}//由于上面的操作已经读到文件末尾此时需要关闭重新打开文件in.close();in.open(p);//任务2std::getline函数的用法std::string name1{};while (!in.eof()){std::getline(in,name1,#);cout name1 endl;}std::cin.get();return 0;}效果 默认情况下getline函数使用换行符作为分隔符