【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.pswp.cn/news/377611.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377611.shtml
英文地址,請注明出處:http://en.pswp.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;…