linux下使用hash_map及STL總結
linux下使用hash_map及STL總結
作者: zhdrfirst 時間: 2010-10-21
出處:http://blog.chinaunix.net/u3/119070/showart_2363418.html
hash_map不是C++標準庫的一部分,但因其重要性很多庫(如sgi stl、boost等)實現了hash_map,包括g++編譯器所帶的標頭檔也包含了hash_map的實現代碼(其實現為sgi stl的版本),其在include/ext目錄下,該目錄還包含了hash_set,rope等的實現。
// 檔案/usr/include/c++/4.4.0/ext/hash_map
_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
65
66 using std::equal_to;
67 using std::allocator;
68 using std::pair;
69 using std::_Select1st;
70
71 /**
72 * This is an SGI extension.
73 * @ingroup SGIextensions
74 * @doctodo
75 */
76 template<class _Key, class _Tp, class _HashFn = hash<_Key>,
77 class _EqualKey = equal_to<_Key>, class _Alloc = allocator<_Tp> >
78 class hash_map
首先從上述標頭檔開始的部分可以發現,hash_map定義在__gnu_cxx命名空間中,故你必須在使用時限定名字空間__gnu_cxx::hash_map,或者使用using關鍵字,如下例:
#include <ext/hash_map>
using namespace __gnu_cxx;
int main()
{
hash_map<int, string> hm;
/* 其它使用hash_map的代碼 */
}
STL其它標頭檔資訊:
1.幾乎所有的容器都在同名的標頭檔裡,比如,vector在<vector>中聲明,list在<list>中聲明等。例外的是<set>和<map>。<set>聲明了set和multiset,<map>聲明了map和multimap。
2. 除了四個演算法外,所有的演算法都在<algorithm>中聲明。例外的是accumulate、inner_product、adjacent_difference和partial_sum。這些演算法在<numeric>中聲明。
3.特殊的迭代器,包括istream_iterators和istreambuf_iterators,在<iterator>中聲明。
4.標準仿函數(比如less<T>)和仿函數適配器(比如not1、bind2nd)在<functional>中聲明。