设为首页 加入收藏

TOP

stl容器学习总结(一)
2014-11-24 00:33:19 来源: 作者: 【 】 浏览:62
Tags:stl 容器 学习 总结

一 迭代器(iterator)

迭代器:
迭代器是类似指针的对象,STL算法利用它们对存储在容器中的对象序列进行遍历。
5种类别:1、输入迭代器
2、输出迭代器
3、前向迭代器
4、双向迭代器
5、随机访问迭代器

常用的迭代器:
istream_iterator< >输入流迭代器
istreambuf_iterator<>输入流块迭代器
ostream_iterator< >输出流迭代器
ostreambuf_iterator<> 输出流块迭代器
back_insert_iterator 使用Container的push_back成员函数
front_insert_iterator 使用Container的push_front成员函数
insert_iterator 使用Container的insert成员函数
reverse_iterator 从后向前使用Container的insert成员函数
const——iterator<>

二 分配算符(Allocators)

看看stl中默认的allocator:

namespace std {
template
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
struct rebind {
typedef allocator 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
allocator(const allocator&) 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::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。


三 容器简介

STL标准容器类简介
标准容器类 说明
顺序性容器
vector 相当与数组,从后面快速的插入与删除,直接访问任何元素
deque 双队列,从前面或后面快速的插入与删除,直接访问任何元素
list 双链表,从任何地方快速插入与删除
关联容器 www.2cto.com
set 快速查找,不允许重复值
multiset 快速查找,允许重复值
map 一对一映射,基于关键字快速查找,不允许重复值
multimap 一对多映射,基于关键字快速查找,允许重复值
容器适配器
stack 后进先出
queue 先进先出
priority_queue 最高优先级元素总是第一个出列

所有标准库共有函数
默认构造函数 提供容器默认初始化的构造函数。
复制构造函数 将容器初始化为现有同类容器副本的构造函数
析构函数 不再需要容器时进行内存整理的析构函数
empty 容器中没有元素时返回true,否则返回false
max_size 返回容器中最大元素个数
size 返回容器中当前元素个数
operator= 将一个容器赋给另一个容器
operator< 如果第一个容器小于第二个容器,返回true,否则返回false,
operator<= 如果第一个容器小于或等于第二个容器,返回true,否则返回false
operator> 如果第一个容器大于第二个容器,返回true,否则返回false
operator>= 如果第一个容器大于或等于第二个容器,返回true,否则返回false
operator== 如果第一个容器等于第二个容器,返回true,否则返回false
operator!= 如果第一个容器不等于第二个容器,返回true,否则返回false
swap 交换两个容器的元素

其中operator>,operator>=,operator<,operator<=,operator==,operator!=均不适用于priority_queue

顺序容器和关联容器共有函数
begin 该函数两个版本返回iterator或const_iterator,引用容器第一个元素
end 该函数两个版本返回iterator或const_iterator,引用容器最后一个元素后面一位
rbegin 该函数两个版本返回reverse_iterator或const_reverse_iterat

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言:常用的预定义的宏简介 下一篇我的C/C++之路(001课)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: