首页 > 代码库 > STL vector中的rbegin方法(5)

STL vector中的rbegin方法(5)

public member function
<vector>

std::vector::rbegin

  • C++98
  • C++11
      reverse_iterator rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept;
Return reverse iterator to reverse beginning
返回一个反向的首元素.
例子:
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
vector<int> vi;
vi.push_back(5);
vi.push_back(999);
cout<<*vi.rbegin()<<endl;




}
运行结果:

可以看到,vi里面元素排列顺序为{5,999},而运用rbegin()返回的迭代器指向的正是最后一个元素的位置,但这个位置和end()的位置是不同的,因为end()指向的是最后一个元素再后面的一个位置(超尾迭代器).

Returns a reverse iterator pointing to the last element in the vector (i.e., its reverse beginning).

返回一个指向最后一个元素的反向迭代器 (相当于从后往前看)


Reverse iterators iterate backwards: increasing them moves them towards the beginning of the container.

反向迭代器的向后迭代:当增加该反向迭代器的时候其实该迭代器是向容器的开头位置移动

例如:

#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
vector<int> vi;
vi.push_back(5);
vi.push_back(999);
vi.push_back(222);
vector<int>::reverse_iterator vr=vi.rbegin();
cout<<*vr<<endl;
vr++;
cout<<*vr<<endl;




}

结果:


可以看到,vr++之后,vr的位置其实是向前移动了!


rbegin points to the element right before the one that would be pointed to by member end.

rbegin指向的元素位置刚好就是end()所指向的前一个。


Notice that unlike member vector::back, which returns a reference to this same element, this function returns a reverse random access iterator.

需要注意的是,不同于back(),back()返回的是一个引用,这个方法返回的是一个反向的随机访问迭代器。


Parameters

none

Return Value

返回值:

A reverse iterator to the reverse beginning of the sequence container.

返回一个反向迭代器指向顺序容器的反向的开头。


If the vector object is const-qualified, the function returns a const_reverse_iterator. Otherwise, it returns a reverse_iterator.

如果这个vector对象具有const属性,那么这个方法返回的iterator也将具有const属性,否则,返回一个普通的reverse_iterator.


Member types reverse_iterator and const_reverse_iterator are reverse random access iterator types (pointing to an element and to a const element, respectively). See vector member types.

reverse_iterator的是一个反向的随机访问迭代器


Example

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

int main ()
{
  std::vector<int> myvector (5);  // 5 default-constructed ints

  std::vector<int>::reverse_iterator rit = myvector.rbegin();

  int i=0;
  for (rit = myvector.rbegin(); rit!= myvector.rend(); ++rit)
    *rit = ++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: 5 4 3 2 1

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可以用来访问以及修改元素,并且都是安全的。


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.

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


//不足之处请多多指导,转载请注明出处:点击打开链接

个人博客主页:点击打开链接

2014-8-9

于GDUT