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

歡迎訪問 生活随笔!

生活随笔

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

c/c++

c++基础学习(10)--(文件、流、异常处理、动态内存、命名空间)

發布時間:2023/12/13 c/c++ 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++基础学习(10)--(文件、流、异常处理、动态内存、命名空间) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 目錄
    • 1.文件和流
    • 2.異常處理
    • 3.動態內存
    • 4.命名空間

目錄

1.文件和流



注意

文件打開方式中的in和out都是相對于內存(計算機)而言的,計算機讀取文件,是將數據從磁盤中的文件讀入到內存中,所以用的是in

#include <fstream> #include <iostream> using namespace std;int main () {char data[100];// 以寫模式打開文件ofstream outfile;outfile.open("afile.dat");cout << "Writing to the file" << endl;cout << "Enter your name: "; cin.getline(data, 100);// 向文件寫入用戶輸入的數據outfile << data << endl;cout << "Enter your age: "; cin >> data;cin.ignore();// 再次向文件寫入用戶輸入的數據outfile << data << endl;// 關閉打開的文件outfile.close();// 以讀模式打開文件ifstream infile; infile.open("afile.dat"); cout << "Reading from the file" << endl; infile >> data; // 在屏幕上寫入數據cout << data << endl;// 再次從文件讀取數據,并顯示它infile >> data; cout << data << endl; // 關閉打開的文件infile.close();return 0; }

當上面的代碼被編譯和執行時,它會產生下列輸入和輸出:
$./a.out
Writing to the file
Enter your name: Zara
Enter your age: 9
Reading from the file
Zara
9
注意

流操作符也是相對于計算機而言的,計算機是可以賦值和改變的左值,在左邊,所以從外部讀取數據到計算機(內存中),使用的流操作符是<< ;同理,從計算機(內存中)輸出到其他設備(屏幕),使用的流操作符是 >>,數據的方向是從內存到外部設備,所以箭頭朝右邊,非常形象。


#include "iostream" #include "fstream" using namespace std;//向文件內部寫入數據,并將數據讀出 void file_wr(void) {char data[100];//向文件寫入數據ofstream outfile;outfile.open("test.txt");cout << "Write to the file" << endl;cout << "Enter your name:" << endl;cin.getline(data, 100);outfile << data << endl;cout << "Enter your age:" << endl;cin >> data;cin.ignore();outfile << data << endl;outfile.close();//從文件讀取數據ifstream infile;infile.open("test.txt");cout << "Read from the file" << endl;infile >> data;cout << data << endl;infile >> data;cout << data << endl;infile.close(); }//將數據從一文件復制到另一文件中 void file_copy(void) {char data[100];ifstream infile;ofstream outfile;infile.open("test.txt");outfile.open("test_1.txt");cout << "copy from test.txt to test_1.txt" << endl;while (!infile.eof()){infile >> data;cout << data << endl;outfile << data << endl;}infile.close();outfile.close(); }//測試上述讀寫文件,與文件數據復制 int main() {file_wr();file_copy();return 0; }

當上面的代碼被編譯和執行時,它會產生下列輸入和輸出:
$./a.out
Writing to the file
Enter your name:
John
Enter your age:
20
Reading from the file
John
20
copy from test.txt to test_1.txt
John
20

2.異常處理



#include <iostream> using namespace std;double division(int a, int b) {if( b == 0 ){throw "Division by zero condition!";}return (a/b); }int main () {int x = 50;int y = 0;double z = 0;try {z = division(x, y);cout << z << endl;}catch (const char* msg) {cerr << msg << endl;}return 0; }

由于我們拋出了一個類型為 const char* 的異常,因此,當捕獲該異常時,我們必須在 catch 塊中使用 const char*。當上面的代碼被編譯和執行時,它會產生下列結果:
Division by zero condition!


3.動態內存


#include <iostream> using namespace std;int main () {double* pvalue = NULL; // 初始化為 null 的指針pvalue = new double; // 為變量請求內存*pvalue = 29494.99; // 在分配的地址存儲值cout << "Value of pvalue : " << *pvalue << endl;delete pvalue; // 釋放內存return 0; }

#include <iostream> using namespace std;int main() {int **p; int i,j; //p[4][8] //開始分配4行8列的二維數據 p = new int *[4];for(i=0;i<4;i++){p[i]=new int [8];}for(i=0; i<4; i++){for(j=0; j<8; j++){p[i][j] = j*i;}} //打印數據 for(i=0; i<4; i++){for(j=0; j<8; j++) { if(j==0) cout<<endl; cout<<p[i][j]<<"\t"; }} //開始釋放申請的堆 for(i=0; i<4; i++){delete [] p[i]; }delete [] p; return 0; }

0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7
0 2 4 6 8 10 12 14
0 3 6 9 12 15 18 21

#include <iostream> using namespace std;class Box {public:Box() { cout << "調用構造函數!" <<endl; }~Box() { cout << "調用析構函數!" <<endl; } };int main( ) {Box* myBoxArray = new Box[4];delete [] myBoxArray; // 刪除數組return 0; }

如果要為一個包含四個 Box 對象的數組分配內存,構造函數將被調用 4 次,同樣地,當刪除這些對象時,析構函數也將被調用相同的次數(4次)。
當上面的代碼被編譯和執行時,它會產生下列結果:

調用構造函數!
調用構造函數!
調用構造函數!
調用構造函數!
調用析構函數!
調用析構函數!
調用析構函數!
調用析構函數!

class A {private:char *m_cBuffer;int m_nLen;public:A(){ m_cBuffer = new char[m_nLen]; }~A() { delete [] m_cBuffer; } }; A *a = new A[10];// 僅釋放了a指針指向的全部內存空間 但是只調用了a[0]對象的析構函數 剩下的從a[1]到a[9]這9個用戶自行分配的m_cBuffer對應內存空間將不能釋放 從而造成內存泄漏 delete a;// 調用使用類對象的析構函數釋放用戶自己分配內存空間并且 釋放了a指針指向的全部內存空間 delete [] a;

4.命名空間

本質上,命名空間就是定義了一個范圍。

#include <iostream> using namespace std;// 第一個命名空間 namespace first_space{void func(){cout << "Inside first_space" << endl;} } // 第二個命名空間 namespace second_space{void func(){cout << "Inside second_space" << endl;} } int main () {// 調用第一個命名空間中的函數first_space::func();// 調用第二個命名空間中的函數second_space::func(); return 0; }

當上面的代碼被編譯和執行時,它會產生下列結果:
Inside first_space
Inside second_space

#include <iostream> using namespace std;// 第一個命名空間 namespace first_space{void func(){cout << "Inside first_space" << endl;} } // 第二個命名空間 namespace second_space{void func(){cout << "Inside second_space" << endl;} } using namespace first_space; int main () {// 調用第一個命名空間中的函數func();return 0; }

當上面的代碼被編譯和執行時,它會產生下列結果:
Inside first_space

using 指令也可以用來指定命名空間中的特定項目。例如,如果您只打算使用 std 命名空間中的 cout 部分,您可以使用如下的語句:

using std::cout;

#include <iostream> using namespace std;// 第一個命名空間 namespace first_space{void func(){cout << "Inside first_space" << endl;}// 第二個命名空間namespace second_space{void func(){cout << "Inside second_space" << endl;}} } using namespace first_space::second_space; int main () {// 調用第二個命名空間中的函數func();return 0; }

當上面的代碼被編譯和執行時,它會產生下列結果:
Inside second_space

總結

以上是生活随笔為你收集整理的c++基础学习(10)--(文件、流、异常处理、动态内存、命名空间)的全部內容,希望文章能夠幫你解決所遇到的問題。

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