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() 用法:在指定地址插入单个元 ...
随机推荐
- Log4Net 使用总结
在项目中要记录日志,便于程序调试.于是就想到了大名鼎鼎的Log4Net,这货可以方便地将日志信息记录到文件.控制台.Windows事件日志和数据库(包括MS SQL Server, Access, O ...
- dubbo架构演变之路
背景 (#) 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 当网站流量很小时, ...
- cocos2d-x 截取屏幕可见区域
在游戏中,我们经常需要分享到社交网络的功能.分享时,我们时常会需要用到截屏的功能.目前网上的文章虽然很多,但是都是截取的 设计分辨率(DesignResolutionSize)大小的屏幕,而这个并不是 ...
- [LeetCode]题解(python):006-ZigZag Conversion
题目来源: https://leetcode.com/problems/zigzag-conversion/ 题意分析: 这道题目是字符串处理的题目.输入一个字符串和一个数字,将字符串填入倒Z形输入字 ...
- 关于block 用法
Block Apple 在C, Objective-C, C++加上Block這個延申用法.目前只有Mac 10.6 和iOS 4有支援.Block是由一堆可執行的程式組成,也可以稱做沒有名字的Fu ...
- jQuery validate和form插件配套使用
参考 官网http://jqueryvalidation.org/documentation/ 博客http://www.cnblogs.com/buzzlight/archive/2010/06/3 ...
- ORA-20000: ORU-10027: buffer overflow, limit of 10000 bytes
要用dbms_output.put_line来输出语句,遇到以下错误: ERROR 位于第 1 行: ORA-20000: ORU-10027: buffer overflow, limit ...
- 射频识别技术漫谈(9)——动物标签HDX
半双工(HDX,Half Duplex)技术是ISO11784/11785中规定的另一种标签与读写器之间的通讯方式.读写器先打开射频场对标签充电以激活标签,然后关闭磁场,标签在读写器磁场关闭的情况下向 ...
- Move WriteBuffer ReadBuffer String
在实际编程中,经常会用到Buffer,当string作为Buffer传值时需要注意的是 1, string的值的起始索引是1,千万记住! 2, 有时候需要先告诉系统去开辟出内存空间,用SetLengt ...
- shouldOverrideUrlLoading相关说明
给WebView加一个事件监听对象(WebViewClient)并重写其中的一些方法:shouldOverrideUrlLoading:对网页中超链接按钮的响应.当按下某个连接时WebViewClie ...