下面我以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. Linux特殊权限:SUID、SGID、SBIT

    SUID:      只对二进制程序有效      执行者对于程序需要有x权限      在程序运行过程中,执行者拥有程序拥有者的权限      例如:      普通用户执行passwd命令.   ...

  2. sql语句开发使用---update

    单表的更新大家都用过了,现在说下实际开发过程中,需要多表的查询更新状态,或者跨数据库的更新状态. 东西需要学习了才会懂得,所以站在巨人的肩膀看的更远. sql 语法; UPDATE 表名称 SET 列 ...

  3. Foundation 框架 NSFileManager,NSData 简单的文件操作

    一.简单展示NSFileManager的使用 #import <Foundation/Foundation.h> int main(int argc, const char * argv[ ...

  4. ECharts的使用相关参考---转

    ECharts图表组件初级入门之(一):如何将ECharts引入至项目中的几种方式 http://www.stepday.com/topic/?801 ECharts图表初级入门篇:如何配置EChar ...

  5. Spring 中拦截器与过滤器的区别

    spring 中拦截器 与servlet 的filter 有相似之处.比如二者都是aop 编程思想的体现都能实现权限检查,日志记录等. 不同之处 使用范围不同 Filter 是Servlet 规定的. ...

  6. iOS __func__标识符

    iOS底层是GCC,所以也支持 __func__标识符,用于打印当前函数名,可以说是排错利器. // data should not be nil nor empty ) {// Exception ...

  7. C++对象模型4--有重写的单继承

    有重写的单继承 派生类中重写了基类的print()函数. //Derived_Overwrite.h #pragma once #include "base.h" class De ...

  8. 5.7.1.2 eval() 方法

    现在我们介绍最后一个方法,这大概是ECMAScript语言中最强大的一个方法:eval().eval()方法就想一个完整的ECMAScript解析器,它只接受一个参数,即要执行的ECMAScript( ...

  9. 【android开发】小说阅读器

    新人开发理念 1 activity 是每个功能页面的入口 2 动画效果需要配合资源文件中,动画的定义 3 文件的读取是有权限控制的 4 布局应该尽量简单,这样才能让程序跑的飞快 前记 重新开始一个新的 ...

  10. 用于COB工艺的PCB设计指导

    绑定角度尽量在45°之内,多于这个角度,绑定时候,银线不好打入焊盘.而且打入焊盘的尾部可能短路到相邻的焊盘,绑定焊盘之间的间距一般在4MIL为极限,半场的工艺一般就这样了.而且焊盘离被绑定的IC引脚最 ...