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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ Primer 5th笔记(1)chapter 1

發(fā)布時(shí)間:2025/3/21 c/c++ 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ Primer 5th笔记(1)chapter 1 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 文件重定向

定義:指定std::cin 和std::out的輸入文件和輸出文件
用法: xx.exe < infileName >outfileName
eg:

#include <iostream> #include "Sales_item.h"int main() {Sales_item item1, item2;std::cin >> item1 >> item2; //read a pair of transactionsstd::cout << item1 + item2 << std::endl; //print their sumreturn 0; }

輸入文件add_Item.input 內(nèi)容為:
0-201-78345-X 3 20.00
0-201-78345-X 2 25.00

執(zhí)行命令:cprimer.exe <…\data\add_Item.input >…\data\additem.output

輸出add_Item.output內(nèi)容為:
0-201-78345-X 5 110 22

2. 另外一個(gè)程序

代碼如下

#include <iostream> #include "Sales_item.h"int main() {Sales_item total; // variable to hold data for the next transaction// read the first transaction and ensure that there are data to processif (std::cin >> total) {Sales_item trans; // variable to hold the running sum// read and process the remaining transactionswhile (std::cin >> trans) {// if we're still processing the same bookif (total.isbn() == trans.isbn()) total += trans; // update the running total else { // print results for the previous book std::cout << total << std::endl; total = trans; // total now refers to the next book}}std::cout << total << std::endl; // print the last transaction} else {// no input! warn the userstd::cerr << "No data?!" << std::endl;return -1; // indicate failure}return 0; }

輸入為:
0-201-70353-X 4 24.99
0-201-82470-1 4 45.39
0-201-88954-4 2 15.00
0-201-88954-4 5 12.00
0-201-88954-4 7 12.00
0-201-88954-4 2 12.00
0-399-82477-1 2 45.39
0-399-82477-1 3 45.39
0-201-78345-X 3 20.00
0-201-78345-X 2 25.00

結(jié)果為:
0-201-70353-X 4 99.96 24.99
0-201-82470-1 4 181.56 45.39
0-201-88954-4 16 198 12.375
0-399-82477-1 5 226.95 45.39
0-201-78345-X 5 110 22

3.一些概念

緩沖區(qū):IO設(shè)施通常將輸入或輸出數(shù)據(jù)保存在一個(gè)緩沖區(qū)中。默認(rèn)情況下,讀cin會(huì)刷新cout;程序非正常終止也會(huì)刷新cout。

clog:一個(gè)ostream對(duì)象,程序的標(biāo)準(zhǔn)錯(cuò)誤信息
Cerr:用于輸出錯(cuò)誤信息或其他不屬于程序正常邏輯的輸出內(nèi)容
cout:ostream:iosteam
cin:istream:iosteam

參考

[1]: 代碼 https://github.com/thefistlei/cplusprimer/tree/main/cprimer

總結(jié)

以上是生活随笔為你收集整理的C++ Primer 5th笔记(1)chapter 1的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。