STL容器的學(xué)習(xí)總結(jié):
第一:迭代器iterator
首先,迭代器的定義,能夠用來遍歷STL容器中的部分或者全部元素,每個(gè)迭代器對象都代表著容易中的確定的地址,迭代器類似于指針類型,修改了常規(guī)指針的接口,是一種概念上的抽象,提供了*,++,==,!=,=操作,這些操作和C/C++操作數(shù)組元素時(shí)的指針接口一致。不同之處是該迭代器是一種smart pointer,具有遍歷復(fù)雜數(shù)據(jù)結(jié)構(gòu)的能力,所有操作行為都使用相同接口,雖然它們的型別不同。迭代器使開發(fā)人員能夠在類或結(jié)構(gòu)中支持foreach迭代
一般分為五種迭代器:輸入迭代器istream_iterator<>和istreambuf_iterator<>,輸出迭代器ostream_iterator<>和ostreambuf_iterator<>,前向迭代器,雙向迭代器,隨機(jī)訪問迭代器
back_insert_iterator<Container> 使用Container的push_back成員函數(shù)
front_insert_iterator<Container> 使用Container的push_front成員函數(shù)
insert_iterator<Container> 使用Container的insert成員函數(shù)
reverse_iterator<Container> 從后向前使用Container的insert成員函數(shù)
const——iterator<>
二 分配算符(Allocators)
看看stl中默認(rèn)的allocator:
namespace std {
template <class T>
class allocator {
public:
//type definitions
typedef size_t size_type; //represent the size of the largest object in the allocation model
typedef ptrdiff_t difference_type; //The type for signed integral values that can represent the distance between any two pointers in the
//allocation model
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type; //The type of the elements
//rebind allocator to type U
template <class U>
struct rebind {
typedef allocator<U> other;
};
//return address of values
pointer address(reference value) const;
const_pointer address(const_reference value) const;
//constructors and destructor
allocator() throw();
allocator(const allocator&) throw();
template <class U>
allocator(const allocator<U>&) throw();
~allocator() throw();
//return maximum number of elements that can be allocated
size_type max_size() const throw();
// allocate but don't initialize num elements of type T
pointer allocate(size_type num,
allocator<void>::const_pointer hint = 0);
// initialize elements of allocated storage p with value value
void construct(pointer p, const T& value);
// delete elements of initialized storage p
void destroy(pointer p);
// deallocate storage p of deleted elements
void deallocate(pointer p, size_type num);
};
}
看了上面的allocator,我們已經(jīng)基本知道他的用處,他一般用在容器中,作為容器的一個(gè)成員,但一般是用模版參數(shù)傳入,這樣才可以讓我們換成我們自定義的allocator。
vector就是動態(tài)數(shù)組,在堆中分配內(nèi)存,元素連續(xù)存放,有保留內(nèi)存,如果減少大小后內(nèi)存也不會釋放。新值大于當(dāng)前大小時(shí)才會再分配內(nèi)存。[]可以使用,隨機(jī)插入,刪除要慢,快速的在末尾插入元素。最重要一點(diǎn)就是它的迭代器會失效。
比如:typedef vector IntArray;
IntArray array;
array.push_back( 1 );
array.push_back( 2 );
array.push_back( 2 );
array.push_back( 3 );
// 刪除array數(shù)組中所有的2
for( IntArray::iterator itor=array.begin(); itor!=array.end(); ++itor )
{
if( 2 == *itor ) array.erase( itor );
}
這樣是不行的,需要按照下面的實(shí)現(xiàn):
for( IntArray::iterator itor=array.begin(); itor!=array.end(); ++itor )
{
if( 2 == *itor )
{
array.erase( itor );
itor--;
}
}
deque,與vector類似,支持隨機(jī)訪問和快速插入刪除。與vector不同的是,deque還支持從開始端插入、刪除數(shù)據(jù)0,[]可以使用,速度沒有vector快。快速的訪問隨機(jī)的元素。快速的在開始和末尾插入元素,重新分配空間后,原有的元
素不需要備份。對deque排序時(shí),可以先將deque的元素復(fù)制到vector,排序后在復(fù)制到deque
list。只能順序訪問不支持隨機(jī)訪問,不存在空間不夠
關(guān)聯(lián)容器:更注重快速和高效的檢索數(shù)據(jù)的能力
set:快速查找,不允許重復(fù)值。
multiset快速查找,允許重復(fù)值。
map:一對一映射,基于關(guān)鍵字快速查找,不允許重復(fù)值,key不能重復(fù)
multimap一對多映射,基于關(guān)鍵字快速查找,允許重復(fù)值
容器適配器:對已有的容器進(jìn)行某些特性的再封裝,
stack:
queue:
(1)獲取向量中的元素值可用三種方式,直接用向量數(shù)組,獲得元素引用,獲得元素的指針。
list:插入操作和刪除操作都不會造成原有的list迭代器失效,每次插入或刪除一個(gè)元素就配置或釋放一個(gè)元素空間,對于任何位置的元素插入或刪除,list永遠(yuǎn)是常數(shù)時(shí)間。
posted on 2011-09-27 17:52
mengkai 閱讀(280)
評論(0) 編輯 收藏 引用