STL vector中的rend方法(10)

2023-10-28 15:50
文章标签 方法 vector stl rend

本文主要是介绍STL vector中的rend方法(10),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文地址:http://www.cplusplus.com/reference/vector/vector/rend/
其实rend和end的区别和rbegin和begin的区别几乎是一样的。
public member function
<vector>

std::vector::rend

  • C++98
  • C++11
      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 a 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 intsstd::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:
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



这篇关于STL vector中的rend方法(10)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/294282

相关文章

检查 Nginx 是否启动的几种方法

《检查Nginx是否启动的几种方法》本文主要介绍了检查Nginx是否启动的几种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1. 使用 systemctl 命令(推荐)2. 使用 service 命令3. 检查进程是否存在4

Java方法重载与重写之同名方法的双面魔法(最新整理)

《Java方法重载与重写之同名方法的双面魔法(最新整理)》文章介绍了Java中的方法重载Overloading和方法重写Overriding的区别联系,方法重载是指在同一个类中,允许存在多个方法名相同... 目录Java方法重载与重写:同名方法的双面魔法方法重载(Overloading):同门师兄弟的不同绝

MySQL字符串转数值的方法全解析

《MySQL字符串转数值的方法全解析》在MySQL开发中,字符串与数值的转换是高频操作,本文从隐式转换原理、显式转换方法、典型场景案例、风险防控四个维度系统梳理,助您精准掌握这一核心技能,需要的朋友可... 目录一、隐式转换:自动但需警惕的&ld编程quo;双刃剑”二、显式转换:三大核心方法详解三、典型场景

MySQL快速复制一张表的四种核心方法(包括表结构和数据)

《MySQL快速复制一张表的四种核心方法(包括表结构和数据)》本文详细介绍了四种复制MySQL表(结构+数据)的方法,并对每种方法进行了对比分析,适用于不同场景和数据量的复制需求,特别是针对超大表(1... 目录一、mysql 复制表(结构+数据)的 4 种核心方法(面试结构化回答)方法 1:CREATE

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

springboot中配置logback-spring.xml的方法

《springboot中配置logback-spring.xml的方法》文章介绍了如何在SpringBoot项目中配置logback-spring.xml文件来进行日志管理,包括如何定义日志输出方式、... 目录一、在src/main/resources目录下,也就是在classpath路径下创建logba

SQL Server中行转列方法详细讲解

《SQLServer中行转列方法详细讲解》SQL行转列、列转行可以帮助我们更方便地处理数据,生成需要的报表和结果集,:本文主要介绍SQLServer中行转列方法的相关资料,需要的朋友可以参考下... 目录前言一、为什么需要行转列二、行转列的基本概念三、使用PIVOT运算符进行行转列1.创建示例数据表并插入数

C++打印 vector的几种方法小结

《C++打印vector的几种方法小结》本文介绍了C++中遍历vector的几种方法,包括使用迭代器、auto关键字、typedef、计数器以及C++11引入的范围基础循环,具有一定的参考价值,感兴... 目录1. 使用迭代器2. 使用 auto (C++11) / typedef / type alias

python项目打包成docker容器镜像的两种方法实现

《python项目打包成docker容器镜像的两种方法实现》本文介绍两种将Python项目打包为Docker镜像的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目录简单版:(一次成功,后续下载对应的软件依赖)第一步:肯定是构建dockerfile,如下:第二步

C# GC回收的方法实现

《C#GC回收的方法实现》本文主要介绍了C#GC回收的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 一、什么是 GC? 二、GC 管理的是哪部分内存? 三、GC 什么时候触发?️ 四、GC 如何判断一个对象是“垃圾