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() 用法:在指定地址插入单个元 ...
随机推荐
- HDU 4740 The Donkey of Gui Zhou (模拟)
由于一开始考虑的很不周到,找到很多bug.....越改越长,不忍直视. 不是写模拟的料...................... 反正撞墙或者碰到已经走过的点就会转向,转向后还碰到这两种情况就会傻站 ...
- java concurrent之前戏synchronized
对于多线程共享资源的情况须要进行同步,以避免一个线程的修改被还有一个线程的修改所覆盖. 最普遍的同步方式就是synchronized. 把代码声明为synchronized.有两个重要后果,一般是指该 ...
- iOS面试题02-数据存储
1.如果后期需要增加数据库中的字段怎么实现,如果不使用CoreData呢? 回答:编写SQL语句来操作原来表中的字段 1>增加表字段 ALETER TABLE 表名 ADD COLUMN 字段名 ...
- c#的Marshal
补充过程中~ 感觉应该是C#调用非托管的比较专门的class 例1. public struct ImageDataMsg { public char DataType; public int Srv ...
- 11.PHP 教程_PHP Switch 语句
switch 语句用于根据多个不同条件执行不同动作. PHP Switch 语句 如果您希望有选择地执行若干代码块之一,请使用 switch 语句. 语法 switch (n) { case labe ...
- excel读入数据库
POI3.9效率大幅度提高,支持xls以及xlsx. 首先需要POI的JAR包,MAVEN配置如下: <!-- excel start --> <dependency> < ...
- verilog中always块延时总结
在上一篇博文中 verilog中连续性赋值中的延时中对assign的延时做了讨论,现在对always块中的延时做一个讨论. 观测下面的程序,@0时刻,输入的数据分别是0x13,0x14 . @2时刻, ...
- SubLime BracketHighlighter 配置
很多插件在github上都有比较详细的说明 告知安装位置什么的一般来说 插件都是放在Packages目录里面的 从Github上下载 解压得到的文件夹然后放入到Packages中这个目录在哪里呢 ...
- 阿里云ECS每天一件事D1:配置SSH
近期因为项目需求,采购了两台阿里云ECS,选择的系统为CentOS 6.3 X64 安全加固版,额外买了160G的硬盘,应该够应付此项目的需求了. ECS默认已经配置好了sshd服务,可以使用root ...
- kinect for windows - 手势识别之一,C++实现
用C++来实现手势识别是比较困难的,所以在这个例子,我们只实现了握拳和松手的手势识别,其他没有实现. 先上个效果图: 在这个程序里,我们打开了kinect的RGB流,深度流,骨骼数据流和手势识别流.其 ...