1、標頭檔案
#ifndef query_h__
#define query_h__
/*文字查詢程式
*/#include #include #include #include #include #include #include using std::vector;
using std::map;
using std::set;
using std::shared_ptr;
using std::string;
using std::ifstream;
using std::ostream;
//前置宣告
class queryresult;
//文字查詢類
class textquery
;//查詢結果類
class queryresult
private:
string m_sought;//查詢的單詞
shared_ptr> m_lines;//這個單詞出現的行號的集合
shared_ptr> m_file;//在哪個檔案中查詢這個單詞
};//函式宣告 列印查詢結果
ostream & print(ostream&, const queryresult&);
#endif // query_h__
2、原始檔
#include "query.h"
#include using std::istringstream;
using std::endl;
textquery::textquery(ifstream &is)
: m_file(new vector)//分配檔案記憶體
lines->insert(n);//新增行號
} }}queryresult textquery::query(const string &sought) const
else }
ostream & print(ostream &os, const queryresult&qr)
return os;
}
3、實現檔案
#include "query.h"
using std::cin;
using std::cout;
using std::endl;
int main()
textquery tq(fin);//文字查詢
fin.close();//關閉檔案
while (true)
print(cout , tq.query(s)) //列印查詢結果
<< endl;
} return 0;
}
C 動態記憶體使用
在c 中,直接對動態記憶體的管理是通過運算子new和delete來完成的。一 使用new動態分配和初始化物件的幾種方式 1.預設情況下,動態分配的物件是預設初始化的,故內建型別或組合型別的物件的值是未定義的,類型別物件將用預設建構函式進行初始化 int pi new int pi指向乙個動態分配的,...
動態記憶體的問題
函式體內的區域性變數在函式結束的時候不會自動消亡 1 指標消亡了,並不代表它所指的記憶體會自動釋放。2 記憶體被釋放了,並不代表指標會消亡或者成為null。野指標 不是null指標,而是指向 非法 記憶體的指標,人們一般不會錯用null指標,因為if語句很容易判斷,但是 野指標 很危險,if語句對它...
動態記憶體的學習
1 物件有可能會被分配到三類空間中 1 靜態記憶體 2 棧記憶體 3 記憶體池 堆 2 物件根據所在不同的空間有兩種不同的建立和銷毀方法 1 前兩種是由編譯器自動建立和銷毀的 2 最後一種的生存期是由程式設計師編寫程式顯式控制的。3 動態記憶體的直接管理由new delete來管理的 容易出問題 4...