原文地址:http://www.cplusplus.com/reference/vector/vector/begin/
public member function
<vector>

std::vector::begin

  • C++98
  • C++11
      iterator begin();
const_iterator begin() const;

Return iterator to beginning

Returns an iterator pointing to the first element in the vector.

该方法返回一个指向该vector中第一个元素的iterator.

Notice that, unlike member vector::front, which returns a reference to the first element, this function returns a random access iterator pointing to it.

需要注意的是,和front()方法不同,front是返回第一个元素的引用,而begin返回的是一个指向第一个元素的随机访问迭代器。

If the container is empty, the returned iterator value shall not be dereferenced.

如果vector是空的,使用begin返回的迭代器不应该被解除引用。

例如:

#include <vector>
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
vector<int> vi;
vector<int>::iterator vb=vi.begin();
cout<<*vb<<endl;
}

编译运行结果:

可以看到,如果对其进行解除引用就会引发错误。

Parameters

none

参数:无

//以后这种超级简单的语句我就不翻译了

Return Value

返回值:

An iterator to the beginning of the sequence container.

指向该顺序容器第一个元素的迭代器。

If the vector object is const-qualified, the function returns a const_iterator. Otherwise, it returns an iterator.

如果该vector是const属性的,那么返回值也将是const属性的,否则,就是返回一个普通的iterator。

Member types iterator and const_iterator are random access iterator types (pointing to an element and to a const element, respectively).

返回值迭代器的类型属于随机访问迭代器。

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// vector::begin/end
#include <iostream>
#include <vector>int main ()
{std::vector<int> myvector;for (int i=1; i<=5; i++) myvector.push_back(i);std::cout << "myvector contains:";for (std::vector<int>::iterator it = myvector.begin() ; it != myvector.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0;
}
Edit & Run

Output:

myvector contains: 1 2 3 4 5

Complexity

Constant.

Iterator validity

No changes.

迭代器的有效性:

调用该方法后,不会影响其他迭代器的有效性。

Data races

数据的竞争性?:(这里可能不太准确)

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

//容器需要支持被访问?(不管返回值是哪个迭代器,都不会修改容器)

No contained elements are accessed by the call, but the iterator returned can be used to access or modify elements. Concurrently accessing or modifying different elements is safe.

//该调用不会访问容器的元素(??),但是返回的这个iterator可以用来访问或者是修改元素,同时通过该iteraotr访问或者是修改元素是安全的。

Exception safety

异常安全性:

No-throw guarantee: this member function never throws exceptions.

该方法不会抛出异常。

The copy construction or assignment of the returned iterator is also guaranteed to never throw.

//这句不知道怎么翻译才最好。

复制构造器或者(assignment of the returned iterator)一样不会抛出异常。

-----------------------------------------------------------------------------------------------------------

//已修正该句://2014-8-9

利用复制构造器或者是赋值运算符得到的该iterator也不会抛出异常。

----------------------------------------------------------------------------------------------------

//翻译的不好的地方请指出来或者联系我修改,谢谢。

//2014-8-9 于GDUT

STL vector中的begin方法(3)相关推荐

  1. Leetcode 1774. Closest Dessert Cost 枚举法 vector 中的insert方法

    vec中每个元素最多选两个 vec拼接成两段,进行枚举,这样就达到目的了 枚举题 class Solution {public:int closestCost(vector<int>&am ...

  2. stl vector 函数_在C ++ STL中使用vector :: begin()和vector :: end()函数打印矢量的所有元素...

    stl vector 函数 打印向量的所有元素 (Printing all elements of a vector) To print all elements of a vector, we ca ...

  3. STL中的nth_element()方法的使用

    STL中的nth_element()方法的使用 通过调用nth_element(start, start+n, end) 方法可以使第n大元素处于第n位置(从0开始,其位置是下标为 n的元素),并且比 ...

  4. vector中的push_back(涉及到动态分配),resize,reserve,swap方法解析

    目录 一.vector的push_back解析 二.size和capacity 三.resize和reserve 1.resize 2.reserve 四.关于reserve的应用 五.内存释放 sw ...

  5. vector容器中重写sort方法

    一:问题描述 1: 我们常用的vector中sort方法是升序的但我们可以通过重新定义sort方法即可实现其的降序处理,以前总是在结构体数组当中用到过重写sort方法,但其实在vector当中也是可以 ...

  6. std::vector中 begin()、end()、front()、back()的区别

    前言 之前看见std::vector 容器的begin().end().front().back()用法,了解begin()和end(),不了解front()和back()方法,今天没事查了下博客,验 ...

  7. stl vector 函数_vector :: clear()函数,以及C ++ STL中的示例

    stl vector 函数 C ++ vector :: clear()函数 (C++ vector::clear() function) vector::clear() is a library f ...

  8. stl vector 函数_vector :: front()函数以及C ++ STL中的示例

    stl vector 函数 C ++ vector :: front()函数 (C++ vector::front() function) vector::front() is a library f ...

  9. C++ STL 迭代器在string类中的使用方法

    //以下例子说明迭代器在string类中的使用方法 #include <iostream> #include <string> #include <algorithm&g ...

最新文章

  1. 条件随机场CRF HMM,MEMM的比较
  2. Hadoop学习13--zookeeper相关
  3. AI目前的根本问题——缺乏 自由意志,无法分辨真正的善恶
  4. 陶哲轩实分析引理10.4.1:反函数定理
  5. brew安装_解决安装brew报的curl的443错误
  6. 计算机无法播放asf格式,ASF文件怎么打开播放出来?
  7. 【android自定义控件】LinearLayout定义ActionBar样式
  8. 【开源电机驱动】H桥基础知识
  9. WES学习2:外显子测序分析——小男孩的硬骨化病基因突变
  10. 汉字转拼音接口 get请求 无需注册
  11. GPU 编程与CG 语言之阳春白雪下里巴人——CG学习读书笔记之数学函数(之二)。
  12. wordpress友联_WordPress添加友情链接功能
  13. android recovery下使用adb出现device offline问题
  14. C语言实现扫雷小游戏(具体步骤+具体说明)
  15. python微博爬虫分析_python爬取和分析新浪微博(一):scrapy构建新浪微博榜单、博主及微博信息爬虫...
  16. 什么是渲染? 通俗易懂的理解
  17. EBS 12.2 开启JWS
  18. centerandzoom 无效_Django调用百度地图api在地图上批量增加标记点
  19. vs2017许可证书过期真正的解决方法
  20. IDEA的校园邮箱激活方式

热门文章

  1. Flask开发微信小程序
  2. 【Python 第7课】if的介绍和使用
  3. 优思学院|六西格玛怎样应用在采购管理之中?
  4. 怎么画六面体网格_Hypermesh 0002 六面体网格画法2
  5. LeetCode 272 Closest Binary Tree Traversal II 解题思路
  6. mysql身份证校验码_用sql实现18位身份证校验代码分享 身份证校验位计算
  7. 四人过桥、三盏灯 三个开关 的答案
  8. android setting 命令,【Android】 adb shell 下 setting 指令
  9. 2017年DARPA战术技术办公室重点关注领域
  10. 工位出租,资源配置平台