随笔 - 15  文章 - 5  trackbacks - 0
<2012年5月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

搜索

  •  

最新评论

  • 1. re: 2011年9月26日[未登录]
  • 我不是吹嘘,为什么1,2,3,4,5,7,9,10,11,12我都知道一点????
    看来我估计可以过电面啊~_~
  • --ZJ
  • 2. re: 2011年9月26日
  • 有计划的人生会很精彩。。
  • --Cheap glueless lace front wigs
  • 3. re: 2011年9月26日
  • (14)举个例子说明你学习能力比较强,
    牛!

    那个腾讯就是做QQ的吧,QQ里面还内嵌个木马,有事没事的扫描下用户磁盘,唉,公司技术就这鸟水平,还对应聘者提那么多要求。
  • --Chipset
  • 4. re: 2011年9月26日
  • 问这么多问题,要求不低啊,呵呵,要回答好需要很扎实的基础
  • --LoveBeyond
  • 5. re: 2011年9月26日
  • 这些问题我十有八九答不上来...惭愧啊
  • --pezy

阅读排行榜

评论排行榜

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 阅读(261) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理