STL-vector

1 篇文章 0 订阅

要想了解更多请参考
http://www.cplusplus.com
cppreference.com
Effective STL  Author: Scott Meyers

 

Why use STL containers ?

More powerful and flexible than arrays,
they grow (and often shrink) dynamically, manage their own memory,
keep track of how many objects they hold,
bound the algorithmic complexity of the operations they support,
and much, much more. Their popularity is easy to understand.
STL containers aren't just good. They're really good.   -- Effective STL


对于一个STL容器,大致要了解的部分一共有
1.Init初始化容器
2.Add添加元素
3.Delete删除元素
4.Access访问元素
5.Find查找元素
6.Traversal遍历
7.Other


vector
头文件    :#include <vector>
逻辑结构:Sequence containers;
物理存储:Contiguous-memory containers。
特点       :组织结构和行为同数组很类似,也可以通过[]访问元素,访问任意元素以及在
                 尾部增删元素都可在O(1)完成,而查找指定的元素以及在特定的位置增删元素(内存拷贝)
                 可以在O(n)内完成。但STL vector很方便的一点是可以动态的resize,shrink,or grow a vector。

适用场景: 如果你需要高效的随即存取,而不在乎插入和删除的效率,使用vector。


类:template < class T, class Alloc = allocator<T> > class vector; // generic template
       template <class Alloc> class vector<bool,Alloc>;                     // bool specialization
      // Avoid using vector<bool>. Bitset maybe better than vector<bool>.
 
类的属性:对于vector的大小,有三个不同属性分别是size(实际存储的element的个数),
                  capacity(当前分配的内存容量),max_size(vector理论上可以存储的最大element个数);

1.实例化对象Init

  1)vector的默认构造函数,初始化一个size=0的vector。
    explicit vector (const allocator_type& alloc = allocator_type());
    example:std::vector<int> first;
 
  2)初始化一个含有n个元素,每个元素值为val的vector。如果val不带,元素值为默认。
    explicit vector (size_type n, const value_type& val = value_type(),
                 const allocator_type& alloc = allocator_type());
    example: std::vector<int> second(4, 12);
 
  3)可以使用已存在的vectorA或者数组,取[first , last)范围内的元素初始化vector。
    template <class InputIterator>
    vector (InputIterator first, InputIterator last,
                 const allocator_type& alloc = allocator_type());
    
    example1:std::vector<int> second(4, 12);
                        std::vector<int> third(second.begin(),second.end());
    
    example2:int myints[] = {16,2,77,29};
                        std::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
                        //注意myints + sizeof(myints) / sizeof(int)指向的为29后面的一个元素(过界)。
    
  4)拷贝构造函数和赋值构造函数
    vector (const vector& x); 
 
    example: std::vector<int> second(4, 12);
                     std::vector<int> four(third);

   vector& operator= (const vector& x);
 
   example: std::vector<int> foo (3,0);
                    std::vector<int> bar (5,0);
                    bar = foo;
                    foo = std::vector<int>();//临时变量   
                    std::cout << "Size of foo: " << int(foo.size()) << '\n'; //0
                    std::cout << "Size of bar: " << int(bar.size()) << '\n'; //3
 
2.Add元素
  1)在尾部添加元素,对于vector的修改在末尾的时候,不需要移动元素,所以效率很高。
    void push_back (const value_type& val);
 
    example: std::vector<int> myvector;
                     int myint;
                     std::cin >> myint;
                     myvector.push_back (myint);
   
  2)在特定的位置插入元素,因为需要移动内存所以效率会有影响。
     (1)iterator insert (iterator position, const value_type& val);
     //返回的iterator会指向新插入的元素val,原来postion指向的元素以及后面的元素会后移
 
    (2)void insert (iterator position, size_type n, const value_type& val);
    //position会指向新插入的n个val的第一个。
 
    (3)template <class InputIterator>
    void insert (iterator position, InputIterator first, InputIterator last);//range[first, last)
    //postion会指向添加的元素[first, last)的first指向的元素
 
    example: std::vector<int> myvector (3,100);  // 100 100 100
                    std::vector<int>::iterator it;

                    it = myvector.begin();
                    it = myvector.insert ( it , 200 );// 200 100 100 100

                    myvector.insert (it,2,300); //300 300 200 100 100 100

 

                   it = myvector.begin();

                   std::vector<int> anothervector (2,400);
                   myvector.insert (it+2,anothervector.begin(),anothervector.end());
                   //300 300 400 400 200 100 100 100
   
                   int myarray [] = { 501,502,503 };
                   myvector.insert (myvector.begin(), myarray, myarray+3);
                   //501 502 503   300 300 400 400 200 100 100 100


3.Delete删除元素
  1)删除结尾的元素
    void pop_back();
 
   example: std::vector<int> myvector;
                   myvector.push_back (100);
                   myvector.push_back (200);
                   myvector.push_back (300);// 100 200 300
   
  2)在特定的位置erase元素
    iterator erase (iterator position); //删除特定位置的一个元素,返回指向下个元素的iterator
    iterator erase (iterator first, iterator last);//删除[first, last)内的元素,返回指向last的iterator

    example: std::vector<int> myvector;

                    for (int i=1; i<=10; i++) myvector.push_back(i);// 1 2 3 4 5 6 7 8 9 10

                    myvector.erase (myvector.begin()+5); // erase 6

                    myvector.erase (myvector.begin(),myvector.begin()+3); //erase 1 2 3
                    //4 5 7 8 9 10

  3)还有一个特殊的方法,删除尾部一定数量的元素,保持头部剩余n个元素。
    void resize (size_type n, value_type val = value_type());
    //当vector的size的值大于n时,才会删除后面多余的元素
 
    example: for (int i=1; i<=10; i++) myvector.push_back(i);// 1 2 3 4 5 6 7 8 9 10
                    myvector.resize(5); //1 2 3 4 5
 
4.Access访问元素
  1)std::vector::operator[]
     reference operator[] (size_type n);
     const_reference operator[] (size_type n) const;
     //如果n>=size 会出现不可预料的错

 

     example: std::vector<int> myvector(10);
                      for (int i=0; i<10; i++) myvector[i] = i+1;// 1 2 3 4 5 6 7 8 9 10
   
   2)std::vector::at,使用at函数访问
      reference at (size_type n);
      const_reference at (size_type n) const;
      //会做bound-checked,如果n>=size 会throw an out_of_range exception
 
     example: std::vector<int> myvector(10);
                     for (int i=0; i<10; i++) myvector.at(i) = i+1;// 1 2 3 4 5 6 7 8 9 10
    
 3)访问特定位置的方法,首std::vector::front,尾std::vector::back。
    reference front();
    const_reference front() const;
     
    reference back();
    const_reference back() const;
  
    example: std::vector<int> myvector;
                    myvector.push_back(78);
                    myvector.push_back(16);
                    myvector.front() -= myvector.back();//62
    
  

5.Find查找元素
6.Traversal遍历
   vector<T>  vec(n);
  1) for(vector<T>::iterator iter=vec.begin(); iter!=vec.end();++iter){}
  2) for(vector<T>::const_iterator iter=vec.begin(); iter!=vec.end();++iter){}//不可修改vec
  3) for(vector<T>::iterator iter=vec.rbegin(); iter!=vec.rend();++iter){}
  4) for(vector<T>::const_iterator iter=vec.rbegin(); iter!=vec.rend();++iter){}//倒序,不可修改vec
  5) for (int i=0; i<vec.size(); ++i){vec[i];}
 
 
7.Other
  1)size_type size(); //返回vector中实际元素的大小size
  2)size_type capacity(); 返回vector当前申请的总的内存大小capacity(capacity>=size)
  3)size_type max_size(); ///返回vector理论上可以存储的元素的个数max_size(max_size >> capacity)
  4)void resize( size_type n, TYPE val );//改变当前vector的size大小为n(可增可减),且对新创建的元素赋值val
  5)void reserve( size_type n );//要求capacity的大小至少为n,当capacity<n时capacity增大为n。
    //其他情况,capacity保持不变。
 
  6)void clear();
  7)bool empty() const; // 优于 size() == 0
  8)std::vector::assign 给vector重新赋值,改变vector的size大小
    (1)template <class InputIterator>
       void assign (InputIterator first, InputIterator last);//[first, last)
    (2)void assign (size_type n, const value_type& val);
   
  9)void swap (vector& x);//x必须是相同类型的,但是size可以不相同。
    //交换当前vector与vector x的元素
 
 
 
 

 

 

 

     
 


 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值