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

相关文章

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

504 Gateway Timeout网关超时的根源及完美解决方法

《504GatewayTimeout网关超时的根源及完美解决方法》在日常开发和运维过程中,504GatewayTimeout错误是常见的网络问题之一,尤其是在使用反向代理(如Nginx)或... 目录引言为什么会出现 504 错误?1. 探索 504 Gateway Timeout 错误的根源 1.1 后端

MySQL 表空却 ibd 文件过大的问题及解决方法

《MySQL表空却ibd文件过大的问题及解决方法》本文给大家介绍MySQL表空却ibd文件过大的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录一、问题背景:表空却 “吃满” 磁盘的怪事二、问题复现:一步步编程还原异常场景1. 准备测试源表与数据

python 线程池顺序执行的方法实现

《python线程池顺序执行的方法实现》在Python中,线程池默认是并发执行任务的,但若需要实现任务的顺序执行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋... 目录方案一:强制单线程(伪顺序执行)方案二:按提交顺序获取结果方案三:任务间依赖控制方案四:队列顺序消

SpringBoot通过main方法启动web项目实践

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring... 目录1. 启动入口:SpringApplication.run()2. SpringApplicat

使用Java读取本地文件并转换为MultipartFile对象的方法

《使用Java读取本地文件并转换为MultipartFile对象的方法》在许多JavaWeb应用中,我们经常会遇到将本地文件上传至服务器或其他系统的需求,在这种场景下,MultipartFile对象非... 目录1. 基本需求2. 自定义 MultipartFile 类3. 实现代码4. 代码解析5. 自定

Python文本相似度计算的方法大全

《Python文本相似度计算的方法大全》文本相似度是指两个文本在内容、结构或语义上的相近程度,通常用0到1之间的数值表示,0表示完全不同,1表示完全相同,本文将深入解析多种文本相似度计算方法,帮助您选... 目录前言什么是文本相似度?1. Levenshtein 距离(编辑距离)核心公式实现示例2. Jac