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

STL 中的back()方法(12)

 
阅读更多
原文地址:http://www.cplusplus.com/reference/vector/vector/back/
front和back我觉得是一对,一个头,一个尾,属性都差不多,详见:http://blog.csdn.net/qq844352155/article/details/38458047

public member function
<vector>

std::vector::back

      reference back();
const_reference back() const;
Access last element
Returns a reference to the last element in thevector.

返回最后一个元素的引用。


Unlike membervector::end, which returns an iterator just past this element, this function returns a direct reference.

和end不一样的是,end是返回一个指向超尾元素的迭代器,而这个函数是直接返回一个引用。


Calling this function on anemptycontainer causes undefined behavior.

对一个空的容器调用该方法会导致未知的行为。

例子:

#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
vector<int> vi;
cout<<vi.back()<<endl;


}

在linux下g++编译运行的结果




Parameters

none

Return value

A reference to the last element in thevector.
返回值是一个指向vector中最后一个元素的引用。

If thevectorobject is const-qualified, the function returns aconst_reference. Otherwise, it returns areference.

如果vector对象本身具有const属性,那么返回的将是const引用,否则是一般的引用。


Member typesreferenceandconst_referenceare the reference types to the elements of thevector(seemember types).

返回值都是属于引用类型。


Example

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

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

  myvector.push_back(10);

  while (myvector.back() != 0)
  {
    myvector.push_back ( myvector.back() -1 );
  }

  std::cout << "myvector contains:";
  for (unsigned i=0; i<myvector.size() ; i++)
    std::cout << ' ' << myvector[i];
  std::cout << '\n';

  return 0;
}


Output:
myvector contains: 10 9 8 7 6 5 4 3 2 1 0

Complexity

Constant.

Iterator validity

No changes.

Data races

The container is accessed (neither the const nor the non-const versions modify the container).

容器将被访问。

The reference returned can be used to access or modify elements. Concurrently accessing or modifying different elements is safe.

返回的引用可以用来访问或者是修改元素,并且这些操作都是安全的。


Exception safety

If the container is notempty, the function never throws exceptions (no-throw guarantee).

如果容器非空,该方法不会抛出异常。

Otherwise, it causesundefined behavior.

如果容器为空,该方法会导致未定义的行为。


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


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

2014-8-12

于GDUT






分享到:
评论

相关推荐

    STL源码剖析.pdg

    1.9.1 stl_config.h 中的各种组态 027 组态3:static template member 027 组态5:class template partial specialization 028 组态6:function template partial order 028 组态7:explicit function template ...

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

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

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

    1.9.1 stl_config.h 中的各种组态 027 组态3:static template member 027 组态5:class template partial specialization 028 组态6:function template partial order 028 组态7:explicit function template ...

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

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

    STL list链表的用法详细解析

    0 前言1 定义一个list2 使用list的成员函数push_back和push_front插入一个元素到list中3 list的成员函数empty()4 用for循环来处理list中的元素5 用STL的通用算法for_each来处理list中的元素6 用STL的通用算法count_...

    vector链表实现,STL

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

    c++ STL之list对结构体的增加,删除,排序等操作详解

    对STL中的list进一步学习,编程过程中对结构体的操作很多。 全部代码如下: /* Project:list对结构体的使用 Date: 2018/07/14 Author: Frank Yu 常用函数:int size() 返回容器元素个数;bool empty() 判断容器...

    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/...

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

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

    Mastering the C++17 .pdf

    The C++ language has a long history, dating back to the 1980s. Recently it has undergone a renaissance, with major new features being intro duced in 2011 and 2014. At press time, the C++17 standard is...

    LKYDeepNN:低依赖性(仅C ++ 11 STL),良好的可移植性,仅头,用于嵌入式的深度神经网络

    LKYDeepNN:低依赖性(仅C ++ 11 STL),良好的可移植性,仅头,用于嵌入式的深度神经网络

    STL常用函数自己总结的

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

    C++程序(类模板)

    C++期末考试可能会考到的代码 类 类模板 模拟STL中的向量,支持用push.back()操作向Vector中加入对象,具有自动resize功能 迭代器

    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 //...

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

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

    C/C++ and Matlab types convertor

    Each time you must convert mxArray to C/C++ types, do some job, and finally convert C/C++ types back to returned mxArray. This project provides easy access to above tasks given appropriate iterators...

    list的用法

    附List用法实例: #include ...//使用STL的max_element算法求listTwo中的最大元素并显示 j=max_element(listTwo.begin(),listTwo.end()); cout (*j); } //输出为: The maximum element in listTwo is: y

    仿QQ日历控件开源项目

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

    Shrey-and-Henry-s-Elves-Shelves-:这基本上是NFC控制的秘密隔间

    什里和亨利的精灵(货架) 项目前期策划 计划前链接 以下是我们项目预规划的一些链接 -我们在Onshape中对我们的项目进行了基本设计,因此我们可以大致了解它的外观以及所有...回购中的一些STL文件: 链接到Onshape文档

Global site tag (gtag.js) - Google Analytics