STLvector中的rend方法(10)


STLvector中的rend方法(10)

其实rend和end的区别和rbegin和begin的区别几乎是一样的。
public member function
<vector>

std::vector::rend

      reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept;
Return reverse iterator to reverse end

Returns a reverse iterator pointing to the theoretical element preceding the first element in the vector (which
is considered its reverse end).

返回一个反向的迭代器指向假设中存在于vector第一个元素前面一个位置的元素(这个元素被认为是反转后的尾部)


The range between vector::rbegin and vector::rend contains all the elements of the vector (in
reverse order).

范围rbegin和rend之间就包含了vector(倒序)的所有元素。


Parameters

none


Return Value

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

一个反向迭代器指向一个序列倒序的超尾。


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

如果vector对象本身具有const属性,那么该函数返回的将是const_reverse_itertor.

否则,返回一个普通的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.
返回迭代器类型为随机访问迭代器

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

Output:

5 4 3 2 1 



我个人测试的例子:

#include <iostream>
#include <vector>
#include <iterator>
#include <initializer_list>
using namespace std;
int main()
{
vector<int> vi({10,20,30,40});
cout<<"Reverse vector:"<<endl;
for(auto it=vi.rbegin();it!=vi.rend();++it)
cout<<*it<<endl;
auto it2=vi.rend();
cout<<"This is values of rend():"<<*it2<<endl;
it2++;
cout<<"This is values of rend()++:"<<*it2<<endl;
auto it3=vi.rend();
it3–;
cout<<"This is values of rend()–:"<<*it3<<endl;


}

运行结果:


可以看到,如果对rend()返回的迭代器直接解除引用,得到的将是默认的0,只有对其进行递减操作(因为rend()已经是倒序的结尾,要想取得其他元素的值,应该递减)


Complexity

Constant.


Iterator validity

No changes.


Data races

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

容器将被访问。(不管是const还是不是,该方法都不会修改容器)


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的拷贝也不会抛出异常。

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

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

2014-8-12

于GDUT


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注