C++ 流類結構


C++ 流 中指的是程式執行緒和 I/O 之間傳輸的字元流。

C++ 中的流類用於對檔案和 I/O 裝置進行輸入和輸出操作。這些類具有特定的功能來處理程式的輸入和輸出。

iostream.h 庫 包含了 C++ 程式語言 中的所有流類。

讓我們看看它們的層次結構並瞭解它們。

現在,讓我們學習一下iostream庫中的類。

ios 類 - 這個類是所有流類的基類。流可以是輸入流或輸出流。此類定義了獨立於類模板定義方式的成員。

istream 類 - istream 類處理 C++ 程式語言中的輸入流。這些輸入流物件用於讀取和解釋輸入作為一系列字元。cin 處理輸入。

ostream 類 - ostream 類處理 C++ 程式語言中的輸出流。這些輸出流物件用於將資料作為一系列字元寫入螢幕。cout 和 puts 處理 C++ 程式語言中的輸出流。

示例

輸出流

COUT

即時演示

#include <iostream>
using namespace std;
int main(){
   cout<<"This output is printed on screen";
}

輸出

This output is printed on screen

PUTS

即時演示

#include <iostream>
using namespace std;
int main(){
   puts("This output is printed using puts");
}

輸出

This output is printed using puts

輸入流

CIN

即時演示

#include <iostream>
using namespace std;
int main(){
   int no;
   cout<<"Enter a number ";
   cin>>no;
   cout<<"Number entered using cin is "<

輸出

Enter a number 3453
Number entered using cin is 3453

gets

#include <iostream>
using namespace std;
int main(){
   char ch[10];
   puts("Enter a character array");
   gets(ch);
   puts("The character array entered using gets is : ");
   puts(ch);
}

輸出

Enter a character array
thdgf
The character array entered using gets is :
thdgf

更新於: 2023-10-26

24K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.