zoukankan      html  css  js  c++  java
  • STL insert()使用

    下面我以vector的insert()为例:

    c++ 98:

    single element (1)
    iterator insert (iterator position, const value_type& val);
    
    fill (2)
        void insert (iterator position, size_type n, const value_type& val);
    
    range (3)
    template <class InputIterator>
        void insert (iterator position, InputIterator first, InputIterator last);

    Insert elements

    The vector is extended by inserting new elements before the element at the specified position, effectively increasing the container size by the number of elements inserted.

    insert()增加了容器的size。

    This causes an automatic reallocation of the allocated storage space if -and only if- the new vector size surpasses the current vector capacity.

    Because vectors use an array as their underlying storage, inserting elements in positions other than the vector end causes the container to relocate all the elements that were after position to their new positions. This is generally an inefficient operation compared to the one performed for the same operation by other kinds of sequence containers (such as list or forward_list).
    因为vector底层使用了array,所以insert()是低效的。

    return value:

    An iterator that points to the first of the newly inserted elements.
    返回指向新插入的元素的迭代器。注意只有

    这种

    insert (iterator position, const value_type& val);
    插入单个元素插入才返回,其他都没有返回


    Member type iterator is a random access iterator type that points to elements.
    If reallocations happen, the storage is allocated using the container's allocator, which may throw exceptions on failure (for the default allocatorbad_alloc is thrown if the allocation request does not succeed).

    #include<iostream>
    #include<vector>
    #include<algorithm>
    using namespace std;
    
    int main()
    {
        vector<int> v(3,100);
        typedef vector<int>::iterator Iter;
        Iter it;
        it=v.begin();
        it=v.insert(it,200);
    
        for(;it!=v.end();it++) //200 100 100 100
             cout<<*it<<ends;
        cout<<endl; 
    
        it=v.begin();
        v.insert(it,2,300);
        //'it' no longer vlaid,get a new one
        
        it=v.begin();
    
        for(;it!=v.end();it++) //300,300,200 100 100 100
             cout<<*it<<ends;
        cout<<endl; 
    
        it=v.begin();
        vector<int> anotherVec(2,400);
        v.insert(it+2,anotherVec.begin(),anotherVec.end());
    
         int myarray [] = { 501,502,503 };
         v.insert (v.begin(), myarray, myarray+3);
    
         for(it=v.begin();it!=v.end();it++)
             cout<<*it<<ends;
         cout<<endl;
    }

    参考;http://www.cplusplus.com/reference/vector/vector/insert/

  • 相关阅读:
    依赖单元测试开发
    今天晚上的遭遇
    设计,UML,测试驱动开发
    我是LIGHT的LP,今天由我代笔
    转贴一篇关于BitVector32的Blog
    看牙记
    调整过的书籍目录
    Queue和Stack的学习代码
    BitVector32结构学习
    Visual Studio 2008 在64位操作系统上调试代码的解决方式
  • 原文地址:https://www.cnblogs.com/youxin/p/3281162.html
Copyright © 2011-2022 走看看