日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

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

發布時間:2023/12/1 c/c++ 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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函數使用換行符作為分隔符

總結

以上是生活随笔為你收集整理的【C++grammar】格式化输出与I/O流函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。