下面我以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(,);
typedef vector<int>::iterator Iter;
Iter it;
it=v.begin();
it=v.insert(it,); for(;it!=v.end();it++) //200 100 100 100
cout<<*it<<ends;
cout<<endl; it=v.begin();
v.insert(it,,);
//'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(,);
v.insert(it+,anotherVec.begin(),anotherVec.end()); int myarray [] = { ,, };
v.insert (v.begin(), myarray, myarray+); for(it=v.begin();it!=v.end();it++)
cout<<*it<<ends;
cout<<endl;
}

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

STL insert()使用的更多相关文章

  1. 省市区三级-sql脚本:

    /*Navicat MySQL Data Transfer Source Server : moiraiSource Server Version : 50631Source Host : 192.1 ...

  2. list源码2(参考STL源码--侯捷):constructor、push_back、insert

    list源码1(参考STL源码--侯捷):list节点.迭代器.数据结构 list源码2(参考STL源码--侯捷):constructor.push_back.insert list源码3(参考STL ...

  3. vector源码3(参考STL源码--侯捷):pop_back、erase、clear、insert

    vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷):空间分配.push_back vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 v ...

  4. STL list 的insert()和erase()

    list 类提供了insert(),erase()函数,它们分别增加和删除一个位于迭代器位置的元素. 1,  insert() iterator insert(iterator pos,const T ...

  5. 【STL】vector的insert方法详解

    #include<vector> #include<iostream> using namespace std; int main() { vector<int>  ...

  6. STL——容器(Set & multiset) insert 的返回值 和 pair 的用法

    1. 使用 insert 插入时的返回值: 将一个元素插入 (insert) 到 set 或 multiset 中时,如果插入失败返回的类型是一个 pair 的自定类型,insert 源码如下: in ...

  7. STL——容器(deque)deque 的插入 insert()

    deque.insert(pos,elem); //在pos位置插入一个elem元素的拷贝,返回新数据的位置. 1 #include <iostream> 2 #include <d ...

  8. STL—— 容器(vector)数据插入insert()方法 的返回值

    vector 容器下的 insert() 方法拥有返回值,由于insert() 方法拥有4种重载函数,他的返回值不尽相同. 第一种,插入单个元素后的返回值: 1 #include <iostre ...

  9. STL—— 容器(vector)的数据插入之 insert()

    vector 容器可以使用 vectorName.insert() 方法插入元素,vectorName.insert() 函数一共有4种重载方法: 第一种 insert() 用法:在指定地址插入单个元 ...

随机推荐

  1. STL中主要的算法(一)

    一.replace() 替换算法将指定元素值替换为新值,使用原型例如以下,将迭代器[first,last)中值为old_value的元素所有替换为new_value值. 函数原型: template  ...

  2. HTML5入门(一)

    HTML简单介绍: HTML(HyperText Markup Language),超文本标记语言,是一种专门用于创建web的超文本文档编程语言,是我们看到的网页的源代码. 版本简介: 1997年推出 ...

  3. POST 方式上传图片

    Post 方式 模仿 form表单 上传 图片 设置enctype = multipart/form-data <form enctype="multipart/form-data&q ...

  4. Lucence.Net学习+盘古分词

    创建索引库          //读取文件,存储到索引库           public string CreateDatebase()         {         //获取索引库的路径   ...

  5. Java 比较两日期相差天数

    (版本1) publicstaticint getIntervalDays(Date fDate, Date oDate) { if (null == fDate || null == oDate) ...

  6. Oracle分区知识

    查询分区名称.表空间的SQL USER_SEGMENTS SELECT SEGMENT_NAME,PARTITION_NAME,TABLESPACE_NAME FROM USER_SEGMENTS; ...

  7. (IOS)悬浮按钮Demo

    思路:传入一个底层的view,将悬浮按钮(用view实现)和展开的子按钮列表add在其上,子按钮列表开始将坐标和悬浮按钮对应好后先将其隐藏,悬浮按钮识别到tap手势后触发展示子按钮列表的方法.通过在t ...

  8. C++ 面向对象学习1

    #include "stdafx.h" #include <iostream> //不要遗漏 否则不能使用cout using namespace std; class ...

  9. ASP.NET MVC进阶之路:依赖注入(Di)和Ninject

    0X1 什么是依赖注入 依赖注入(Dependency Injection),是这样一个过程:某客户类只依赖于服务类的一个接口,而不依赖于具体服务类,所以客户类只定义一个注入点.在程序运行过程中,客户 ...

  10. Javascript中的位运算符和技巧

    ECMAScript 整数有两种类型,即有符号整数(允许用正数和负数)和无符号整数(只允许用正数).在 ECMAScript 中,所有整数字面量默认都是有符号整数,这意味着什么呢? 有符号整数使用 3 ...