STL vector中的begin方法(3)

原文地址:http://www.cplusplus.com/reference/vector/vector/begin/

public member function
<vector>

std::vector::begin

      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;
}


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


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL vector是C++标准模板库(STL的容器之一,用于存储和管理动态大小的元素序列。它提供了一些便利的操作和功能,比如自动调整大小、快速随机访问、插入和删除元素等。 你可以使用vector来存储任何类型的元素,包括基本数据类型和自定义对象。vector使用连续的内存来存储元素,并支持在常量时间内访问任意位置的元素。 下面是一些常用的vector操作: 1. 创建vector: 可以使用默认构造函数或将现有的序列作为参数传递给构造函数来创建vector对象。 ```cpp #include <vector> std::vector<int> numbers; // 声明一个vector std::vector<int> numbers = {1, 2, 3, 4}; // 使用初始值列表创建vector ``` 2. 访问元素: 可以使用下标运算符([])来访问vector的元素。 ```cpp std::vector<int> numbers = {1, 2, 3, 4}; int firstElement = numbers[0]; // 访问第一个元素 int lastElement = numbers[numbers.size() - 1]; // 访问最后一个元素 ``` 3. 添加和删除元素: 可以使用push_back()函数将元素添加到vector的末尾,并使用pop_back()函数删除末尾的元素。 ```cpp std::vector<int> numbers; numbers.push_back(1); // 在末尾添加元素 numbers.pop_back(); // 删除末尾的元素 ``` 4. 大小操作: 可以使用size()函数获取vector元素的数量,并使用empty()函数检查vector是否为。 ```cpp std::vector<int> numbers = {1, 2, 3, 4}; int size = numbers.size(); // 获取元素数量 bool isEmpty = numbers.empty(); // 检查是否为 ``` 5. 迭代器: 可以使用迭代器遍历vector的元素。 ```cpp std::vector<int> numbers = {1, 2, 3, 4}; for (auto it = numbers.begin(); it != numbers.end(); ++it) { std::cout << *it << " "; // 输出每个元素 } ``` 这些只是vector的一些基本操作,STL vector还提供了许多其他功能和算法,可以根据需求进行使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值