STL容器的學習總結:
第一:迭代器iterator
首先,迭代器的定義,能夠用來遍歷STL容器中的部分或者全部元素,每個迭代器對象都代表著容易中的確定的地址,迭代器類似于指針類型,修改了常規指針的接口,是一種概念上的抽象,提供了*,++,==,!=,=操作,這些操作和C/C++操作數組元素時的指針接口一致。不同之處是該迭代器是一種smart pointer,具有遍歷復雜數據結構的能力,所有操作行為都使用相同接口,雖然它們的型別不同。迭代器使開發人員能夠在類或結構中支持foreach迭代
一般分為五種迭代器:輸入迭代器istream_iterator<>和istreambuf_iterator<>,輸出迭代器ostream_iterator<>和ostreambuf_iterator<>,前向迭代器,雙向迭代器,隨機訪問迭代器
back_insert_iterator<Container> 使用Container的push_back成員函數
front_insert_iterator<Container> 使用Container的push_front成員函數
insert_iterator<Container> 使用Container的insert成員函數
reverse_iterator<Container> 從后向前使用Container的insert成員函數
const——iterator<>
二 分配算符(Allocators)
看看stl中默認的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,我們已經基本知道他的用處,他一般用在容器中,作為容器的一個成員,但一般是用模版參數傳入,這樣才可以讓我們換成我們自定義的allocator。
vector就是動態數組,在堆中分配內存,元素連續存放,有保留內存,如果減少大小后內存也不會釋放。新值大于當前大小時才會再分配內存。[]可以使用,隨機插入,刪除要慢,快速的在末尾插入元素。最重要一點就是它的迭代器會失效。
比如:typedef vector IntArray;
IntArray array;
array.push_back( 1 );
array.push_back( 2 );
array.push_back( 2 );
array.push_back( 3 );
// 刪除array數組中所有的2
for( IntArray::iterator itor=array.begin(); itor!=array.end(); ++itor )
{
if( 2 == *itor ) array.erase( itor );
}
這樣是不行的,需要按照下面的實現:
for( IntArray::iterator itor=array.begin(); itor!=array.end(); ++itor )
{
if( 2 == *itor )
{
array.erase( itor );
itor--;
}
}
deque,與vector類似,支持隨機訪問和快速插入刪除。與vector不同的是,deque還支持從開始端插入、刪除數據0,[]可以使用,速度沒有vector快。快速的訪問隨機的元素。快速的在開始和末尾插入元素,重新分配空間后,原有的元
素不需要備份。對deque排序時,可以先將deque的元素復制到vector,排序后在復制到deque
list。只能順序訪問不支持隨機訪問,不存在空間不夠
關聯容器:更注重快速和高效的檢索數據的能力
set:快速查找,不允許重復值。
multiset快速查找,允許重復值。
map:一對一映射,基于關鍵字快速查找,不允許重復值,key不能重復
multimap一對多映射,基于關鍵字快速查找,允許重復值
容器適配器:對已有的容器進行某些特性的再封裝,
stack:
queue:
(1)獲取向量中的元素值可用三種方式,直接用向量數組,獲得元素引用,獲得元素的指針。
list:插入操作和刪除操作都不會造成原有的list迭代器失效,每次插入或刪除一個元素就配置或釋放一個元素空間,對于任何位置的元素插入或刪除,list永遠是常數時間。
posted on 2011-09-27 17:52
mengkai 閱讀(281)
評論(0) 編輯 收藏 引用