【C++grammar】格式化输出与I/O流函数

目录

  • 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;
输出:
_ _a

setw()控制符只对其后输出的第一个数据有效

std::cout << std::setw(5) << 'a'<< 'b' << std::endl;
输出:
_ _ _ _ab

setw()的默认为setw(0),按实际输出。
如果输出的数值占用的宽度超过setw(int n)设置的宽度,则按实际宽度输出。

float f=0.12345;std::cout << std::setw(3) << f << std::endl;
输出:
0.12345

2. 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;
输出:
****a

4. Formatting Output in File Operation(在文件操作中格式化输入/输出)

在这里插入图片描述

5.小练习

本部分展示内容如下;
任务1:展示setw和setfill
1、setw只对紧跟随其后的数据起作用
2、setfill指定填充字符
任务2:展示setprecision、fixed、showpoint、left、right
任务3:展示hexfloat

#include <iostream>
#include <iomanip>using 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(i+2) << ' ' << 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::array<char , 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_cast<char>(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 << "Can't open file" << p << endl;std::abort();}//任务1:istream::getline函数constexpr int SIZE = 1024;std::array<char, SIZE> buf;	//&bufwhile (!in.eof()){in.getline(&buf[0], SIZE, '#');cout << &buf[0] << endl;}//由于上面的操作已经读到文件末尾,此时需要关闭重新打开文件in.close();in.open(p);//任务2:std::getline函数的用法std::string name1{""};while (!in.eof()){std::getline(in,name1,'#');cout << name1 << endl;}std::cin.get();return 0;}

效果:
在这里插入图片描述
默认情况下,getline函数使用换行符作为分隔符

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377611.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python 忽略 异常_如何忽略Python中的异常?

python 忽略 异常什么是例外&#xff1f; (What is an Exception?) An exception is an event, which occurs during the execution of a program that interrupts the normal execution of the application. Generally, any application when encountered with a situation t…

三、实战---爬取百度指定词条所对应的结果页面(一个简单的页面采集器)

在第一篇博文中也提及到User-Agent&#xff0c;表示请求载体的身份&#xff0c;也就是说明通过什么浏览器进行访问服务器的&#xff0c;这一点很重要。 ① UA检测 门户网站服务器会检测请求载体的身份。如果检测到载体的身份表示为某一款浏览器的请求&#xff0c;则说明这是一…

Spring MVC拦截器实现分析

SpringMVC的拦截器不同于Spring的拦截器&#xff0c;SpringMVC具有统一的入口DispatcherServlet&#xff0c;所有的请求都通过DispatcherServlet&#xff0c;所以只需要在DispatcherServlet上做文章即可&#xff0c;DispatcherServlet也没有代理&#xff0c;同时SpringMVC管理的…

硕士毕业后去国外读法学博士_法学硕士的完整形式是什么?

硕士毕业后去国外读法学博士法学硕士&#xff1a;豆科大法师(拉丁)/法学硕士 (LLM: Legum Magister (Latin)/ Master of Law) LLM is an abbreviation of Legum Magister. It is in term of Latin which states the masters degree of Law. In the majority, LLM is generally …

android:layout_weight属性的简单使用

效果&#xff1a; style.xml <style name"etStyle2"><item name"android:layout_width">match_parent</item><item name"android:layout_height">wrap_content</item><item name"android:background"…

一、环境配置安装

一、Anaconda Ⅰ下载 最新版的anaconda可能会需要各种各样的问题&#xff0c;python3.6版本比较稳定&#xff0c;建议使用。 老铁们可以通过&#xff0c;Anaconda以前版本所自带Python版本&#xff0c;查看Anaconda所带的python版本 我用的是这个&#xff0c;Anaconda3-5.2.0…

leetcode 35. 搜索插入位置 思考分析

目录题目暴力二分迭代二分递归题目 给定一个排序数组和一个目标值&#xff0c;在数组中找到目标值&#xff0c;并返回其索引。如果目标值不存在于数组中&#xff0c;返回它将会被按顺序插入的位置。 你可以假设数组中无重复元素。 示例 1: 输入: [1,3,5,6], 5 输出: 2 示例 2:…

java优秀算法河内之塔_河内塔的Java程序

java优秀算法河内之塔Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move all disks from source rod to destination rod using the third rod (say auxiliary). The rules are: 河内塔是一个数学难题&a…

转——C# DataGridView控件 动态添加新行

DataGridView控件在实际应用中非常实用&#xff0c;特别需要表格显示数据时。可以静态绑定数据源&#xff0c;这样就自动为DataGridView控件添加相应的行。假如需要动态为DataGridView控件添加新行&#xff0c;方法有很多种&#xff0c;下面简单介绍如何为DataGridView控件动态…

分享通用基类库-C#通用缓存类

1 /************************************************************************************* 2 * 代码:吴蒋 3 * 时间:2012.03.30 4 * 说明:缓存公共基类 5 * 其他: 6 * 修改人&#xff1a; 7 * 修改时间&#xff1a; 8 * 修改说明&#xff1a; 9 ******************…

二、PyTorch加载数据

一、常用的两个函数 dir()函数可以理解为打开某个包&#xff0c;help()可以理解为返回如何使用某个具体的方法 例如&#xff1a;若一个A钱包里面有a&#xff0c;b&#xff0c;c&#xff0c;d四个小包&#xff0c;则可通过dir(A)&#xff0c;打开该A钱包&#xff0c;返回a&…

leetcode 1005. K 次取反后最大化的数组和 思考分析

题目 给定一个整数数组 A&#xff0c;我们只能用以下方法修改该数组&#xff1a;我们选择某个索引 i 并将 A[i] 替换为 -A[i]&#xff0c;然后总共重复这个过程 K 次。&#xff08;我们可以多次选择同一个索引 i。&#xff09; 以这种方式修改数组后&#xff0c;返回数组可能…

三、TensorBoard

一、安装TensorBoard 管理员身份运行Anaconda Prompt&#xff0c;进入自己的环境环境 conda activate y_pytorch&#xff0c;pip install tensorboard 进行下载&#xff0c;也可以通过conda install tensorboard进行下载。其实通俗点&#xff0c;pip相当于菜市场&#xff0c;c…

IT资产管理系统SQL版

你难道还在用Excel登记IT资产信息吗&#xff1f; 那你一定要好好考虑如何面对以下问题 1&#xff1a;IT人员需要面对自身部门以下问题用户申请了资产it部未处理的单还有哪些?库存里面还有哪些资产?有多少设备在维修?有多少设备已经报废了?哪些资产低于安全库存需要采购?使…

详细讲解设计跳表的三个步骤(查找、插入、删除)

目录写在前面跳表概要查找步骤插入步骤删除步骤完整代码写在前面 关于跳表的一些知识可以参考这篇文章,最好是先看完这篇文章再看详细的思路->代码的复现步骤: Redis内部数据结构详解(6)——skiplist 关于跳表的插入、删除基本操作其实也就是链表的插入和删除&#xff0c;所…

php 类静态变量 和 常量消耗内存及时间对比

在对类执行100w次循环后&#xff0c; 常量最快&#xff0c;变量其次&#xff0c;静态变量消耗时间最高 其中&#xff1a; 常量消耗&#xff1a;101.1739毫秒 变量消耗&#xff1a;2039.7689毫秒 静态变量消耗&#xff1a;4084.8911毫秒 测试代码&#xff1a; class Timer_profi…

一个机器周期 计算机_计算机科学组织| 机器周期

一个机器周期 计算机机器周期 (Machine Cycle) The cycle during which a machine language instruction is executed by the processor of the computer system is known as the machine cycle. If a program contains 10 machine language instruction, 10 separate machine …

四、Transforms

transform是torchvision下的一个.py文件&#xff0c;这个python文件中定义了很多的类和方法&#xff0c;主要实现对图片进行一些变换操作 一、Transforms讲解 from torchvision import transforms#按着Ctrl&#xff0c;点击transforms进入到__init__.py文件中 from .transfo…

leetcode 134. 加油站 思考分析

目录题目1、暴力法&#xff0c;双层遍历2、贪心题目 在一条环路上有 N 个加油站&#xff0c;其中第 i 个加油站有汽油 gas[i] 升。 你有一辆油箱容量无限的的汽车&#xff0c;从第 i 个加油站开往第 i1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发&#xff0…

单链线性表的实现

//函数结果状态代码#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 //Status是函数的类型&#xff0c;其值是函数结果状态代码 typedef int Status; typedef int ElemType;…