`
阿尔萨斯
  • 浏览: 4243290 次
社区版块
存档分类
最新评论

STL vector中的push_back方法(17)

 
阅读更多

public member function
<vector>

std::vector::push_back

void push_back (const value_type& val);
void push_back (value_type&& val);
Add element at the end
Adds a new element at the end of thevector, after its current last element. The content ofvalis copied (or moved) to the new element.
该函数将一个新的元素加到vector的最后面,位置为当前最后一个元素的下一个元素,新的元素的值是val的拷贝(或者是移动拷贝).

This effectively increases the containersizeby one, which causes an automatic reallocation of the allocated storage space if -and only if- the new vectorsizesurpasses the current vectorcapacity.
该方法可以快速有效率地在数组size范围内增长元素,除非当增长的元素个数大小超出了vector的ccapacity的时候才会发生重分配。

Parameters

val
Value to be copied (or moved) to the new element.
Member typevalue_typeis the type of the elements in the container, defined invectoras an alias of its first template parameter (T).
参数
新元素的值。
类型由vector的模版参数指定。

Return value

none

If a reallocation happens, the storage is allocated using the container'sallocator, which may throw exceptions on failure (for the defaultallocator,bad_allocis thrown if the allocation request does not succeed).
如果发生了重分配,将使用容器的分配器进行内存分配,这可能会抛出异常。(例如allocator这个默认的分配器在请求失败时会抛出bad_alloc异常)

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// vector::push_back
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector;
  int myint;

  std::cout << "Please enter some integers (enter 0 to end):\n";

  do {
    std::cin >> myint;
    myvector.push_back (myint);
  } while (myint);

  std::cout << "myvector stores " << int(myvector.size()) << " numbers.\n";

  return 0;
}

The example usespush_backto add a new element to the vector each time a new integer is read.这个例子里面,每当读取一个整数输入时,都使用push_back来将其加入到vector中。

Complexity

Constant (amortized time, reallocation may happen).

If a reallocation happens, the reallocation is itself up to linear in the entiresize.
如果发生重分配,重分配过程是线性是时间复杂度。

Iterator validity

If a reallocation happens, all iterators, pointers and references related to the container are invalidated.如果发生重分配,之前所获得的所有迭代器,指针以及引用都将失效。
Otherwise, only theend iteratoris invalidated, and all iterators, pointers and references to elements are guaranteed to keep referring to the same elements they were referring to before the call.
否则,只有超尾迭代器失效,之前所获得的其他迭代器仍然有效。

Data races

The container is modified.
If a reallocation happens, all contained elements are modified.
Otherwise, no existing element is accessed, and concurrently accessing or modifying them is safe.
容器将被修改。
如果发生重分配,所有容器内元素都将被修改。
否则,不会访问容器内元素,同时访问以及修改他们都是安全的。

Exception safety

If no reallocations happen, there are no changes in the container in case of exception (strong guarantee).如果没有发生重分配,容器抛出异常的规则不变。
If a reallocation happens, the strong guarantee is also given if the type of the elements is eithercopyableorno-throw moveable.如果发生重分配,如果元素类型的复制构造器以及移动构造器不会抛出异常,那么规则也不变
Otherwise, the container is guaranteed to end in a valid state (basic guarantee).
Ifallocator_traits::constructis not supported withvalas argument, it causesundefined behavior.
否则,容器只保证在一个有效的状态下结束。(???)如果allocator_traits::construct不支持val(???)。将会导致为定义的行为。

//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。


转载请注明出处:http://blog.csdn.net/qq844352155

2014-8-14

于GDUT





分享到:
评论

相关推荐

    C++ push方法与push_back方法的使用与区别

    push与push_back是STL中常见的方法,都是向数据结构中添加元素。初识STL,对于添加元素的方法以产生混淆,这里暂对两种方法作出比较分析。此外,本文还将简述push对应的stack与queue系列,常见方法的介绍,以及与...

    vector链表实现,STL

    如果将节点变为数组,将会常熟优化。vector链表实现。...拥有iterator,begin,end,rbegin,rend,operator[],push_back,pop_back,push_front,pop_front,size等海量函数以及STL函数支持,也支持RE判断!

    C++之STL的vector详解,包括初始化和各种函数:vector的初始化、数据的增删查改等

    6、尾部添加 push_back(ele) 7、尾部删去 pop_back() 8、删区间 9、删指定位置 10、清空 四、其他接口 1、size()成员函数 2、empty() 3、resize(int num) 4、capacity() 5、reserve(int len) 总结

    c++STL基本容器用法带程序详解

    vector用于存储对象数组 常用方法 1.push_back 在数组的最后添加一个数据 2.pop_back 去掉数组的最后一个数据 3.at 得到编号位置的数据 4.begin 得到数组头的指针 5.end 得到数组的最后一个单元+1的指针 6.front ...

    STL源码剖析.pdg

    4.2.5 vector 的构造与内存管理:constructor, push_back 119 4.2.6 vector 的元素操作:pop_back, erase, clear, insert 123 4.3 list 128 4.3.1 list 概述 128 4.3.2 list 的节点(node) 129 4.3.3 list 的...

    STL 源码剖析(侯捷先生译著)

    4.2.5 vector 的构造与内存管理:constructor, push_back 119 4.2.6 vector 的元素操作:pop_back, erase, clear, insert 123 4.3 list 128 4.3.1 list 概述 128 4.3.2 list 的节点(node) 129 4.3.3 list 的...

    C++标准库使用范例

    *范例编号:17_39 *范例说明:演示 unique 的 * 功能与使用方法 ***************************/ #include &lt;vector&gt; #include &lt;algorithm&gt; #include &lt;functional&gt; #include &lt;iostream&gt; #include &lt;ostream&gt; using ...

    C++ Vector用法详解

    vector是C++标准模版库(STL,Standard Template Library)中的部分内容。之所以认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单的说:vector是一个能够存放任意类型的动态数组,能够增加和压缩数据。...

    DStl:采用STL样式的各种数据结构实现

    Deque和CircularQueue都是双向队列的实现,其中Deque符合stl对deque的规范,"push_front, push_back, emplace_front and emplace_back do not invalidate any references to elements of the deque.",这是Vector/...

    C++11的for循环,以及范围Range类的简单实现

    C++11支持range-based for循环。这是一个很方便的特性,能省挺多代码。以下代码就能很方便的遍历vector中的元素,并打印出来: std::vector&lt;int&gt; int_vec; int_vec.push_back(1);...(STL 中所有容器都可

    C++ vector使用的一些注意事项

    1. 初始化  c++ 11以后新增了大括号{}的初始化方式,... 通过push_back添加新的元素进入vector后,vector的内存有时候会发生变化,这取决于size和capacity大小,当然这些都是系统来处理的,详细可以参考stl源码  

    STL常用函数

    words.push_back(s); //获取vector大小 words.size(); //遍历vector for(int i=0;i&lt;word.size();i++) //注意,它和数组一样,下标从0开始 //用sort对vector内元素排序 sort(words.begin(),words.end()); set //...

    HumanMotionTrack 全

    //放入vector结构,这里用到了STL编程技术中的vector 以下是实现的部分代码: skelecton::skelecton() { float fy = 0.56f ; float ftx = 0.19f; float ffx = 0.08f; bone obone = bone (1,"neck",0); ...

    TinySTL:参考侯捷的STL原始码解析,实现的基础的STL容器-源码解析

    提供size() , empty() , push_back() , pop_back()等接口并提供迭代器访问 放 尽快使用BST实现,后面会改成AVL树或者RB树,空间配置使用SimpleAllocate.h 提供前后访问的迭代器 已经改用AVL树实现 堆 极端使用...

    仿QQ日历控件开源项目

    例如显示月份的单元格是由vector动态的push_back,但是如果你认真分析后会发现实际上这些单元格是固定的,也就是说在构造函数中就可以new一个固定大小的内存空间来供vector使用,而不用每次都动态的push_back。...

    c++中vectorlt;intgt;和vectorlt;int*gt;的用法区别

    在使用STL容器(比如map、list、vector等)的时候,是用放一个对象还是放一个对象指针,即是用vector还是vector,这里的vector可以换成其他的容器,int可以换成其他基本类型,也可以自定义的数据结构或类。...

    STL常用函数自己总结的

    总结了一些使用多的函数,可以快速学习和使用。vector&lt;int&gt;::iterator it=b.begin(); vec.push_back(t); vec.pop_back(t);

    File-Vector:用于非常快速的列数据库的 C++ 文件支持向量,对时间序列数据很有用

    文件矢量 用于非常快速的列数据库的 C++ 文件支持向量,对时间序列数据很有用。 该 API 与 STL 向量类相同,只是为构造函数提供了一... 有包括 push_back 在内的完整写支持,并且使用通常的向量加倍算法保留文件空间。

    leetcode添加元素使和等于-step_LeetCode:算法与数据结构(含经典LeetCode题目)

    leetcode添加元素使和等于 [TOC] 基础内容 指针与引用 函数 STL标准库 algorithm算法 #include // ...若在线平台不让加头文件,用... test.push_back(7); //尾部加入元素 reverse(test.begin(), test.end()); //反转vec

    containers:我使用的容器的集合

    建置状态 macOS / Linux / Windows ...使用“ push_back”时,速度比std :: vector快约3倍,比eastl :: vector快约2倍 否则表现与其他相同 jc /测试 一个小型的C / C ++测试框架: 少于1kloc。 极

Global site tag (gtag.js) - Google Analytics