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> |
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 allocator, bad_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()使用的更多相关文章
- 省市区三级-sql脚本:
/*Navicat MySQL Data Transfer Source Server : moiraiSource Server Version : 50631Source Host : 192.1 ...
- list源码2(参考STL源码--侯捷):constructor、push_back、insert
list源码1(参考STL源码--侯捷):list节点.迭代器.数据结构 list源码2(参考STL源码--侯捷):constructor.push_back.insert list源码3(参考STL ...
- vector源码3(参考STL源码--侯捷):pop_back、erase、clear、insert
vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷):空间分配.push_back vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 v ...
- STL list 的insert()和erase()
list 类提供了insert(),erase()函数,它们分别增加和删除一个位于迭代器位置的元素. 1, insert() iterator insert(iterator pos,const T ...
- 【STL】vector的insert方法详解
#include<vector> #include<iostream> using namespace std; int main() { vector<int> ...
- STL——容器(Set & multiset) insert 的返回值 和 pair 的用法
1. 使用 insert 插入时的返回值: 将一个元素插入 (insert) 到 set 或 multiset 中时,如果插入失败返回的类型是一个 pair 的自定类型,insert 源码如下: in ...
- STL——容器(deque)deque 的插入 insert()
deque.insert(pos,elem); //在pos位置插入一个elem元素的拷贝,返回新数据的位置. 1 #include <iostream> 2 #include <d ...
- STL—— 容器(vector)数据插入insert()方法 的返回值
vector 容器下的 insert() 方法拥有返回值,由于insert() 方法拥有4种重载函数,他的返回值不尽相同. 第一种,插入单个元素后的返回值: 1 #include <iostre ...
- STL—— 容器(vector)的数据插入之 insert()
vector 容器可以使用 vectorName.insert() 方法插入元素,vectorName.insert() 函数一共有4种重载方法: 第一种 insert() 用法:在指定地址插入单个元 ...
随机推荐
- [Ext JS 4] 布局之实战一 - 中间区块不会自动伸展 (tab)
前言 [Ext JS 4] 布局之实战一 - 中间区块不会自动伸展 (tab) 在上一篇中,中间的tab 区块无法自动伸展的原因一句话说就是: 使用contentEL的方式,相关HTML元素不会参与组 ...
- .Net Web开发中实现剪切板功能
我要实现的功能是:在列表页,通过一个按钮复制对应的文章Url,如下图: 如下代码: <a class="btn btn-success copy" href=&quo ...
- SQL数据库关键字和列名冲突处理
在设计SQL数据库的时候可能由于考虑不全,使列名和数据库内关键字冲突,可能导致Query不能被正确识别,对列名要加[]处理.
- Linux新手笔记 源 安装chromium
centos6.4 32 一.软件源目录/etc/yum.repos.d把新的软件源文件copy到这即可.二.安装chromiumwget http://people.centos.org/hughe ...
- #pragma pack(push,1)与#pragma pack(pop)
这是给编译器用的参数设置,有关结构体字节对齐方式设置, #pragma pack是指定数据在内存中的对齐方式. #pragma pack (n) 作用:C编译器将按照n个字节对 ...
- Python学习笔记 (4) :迭代器、生成器、装饰器、递归、正则表达式等
迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外,迭代器的一大优点是 ...
- [转] iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用
介绍: Grand Central Dispatch 简称(GCD)是苹果公司开发的技术,以优化的应用程序支持多核心处理器和其他的对称多处理系统的系统.这建立在任务并行执行的线程池模式的基础上的.它首 ...
- php文件链接数据库基本代码
<?php $conn=@mysql_connect("localhost","root",""); if($conn==null) ...
- Windows10笔记本双显卡导致的启动黑屏解决办法之一
参考链接:http://www.zhihu.com/question/33662311 大概就是关掉ulps. ulps,显卡的多核心超低功率状态,节能用的,AMD出的双显卡的一种节能方案.不过,与某 ...
- 基于Visual C++2013拆解世界五百强面试题--题9-找出所有的排列方式
给出一个函数来输出一个字符串的所有排列 按照排列组合的知识我们知道 N个字符排列组合个数有n!种, 那么可知f(n) = n*f(n-1), 如果{1,2}的组合有两种,12,21, 那么{123}的 ...