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 413修改上传文件大小限制的方法详解

《Nginx413修改上传文件大小限制的方法详解》在使用Nginx作为Web服务器时,有时会遇到客户端尝试上传大文件时返回​​413RequestEntityTooLarge​​... 目录1. 理解 ​​413 Request Entity Too Large​​ 错误2. 修改 Nginx 配置2.1

使用@Cacheable注解Redis时Redis宕机或其他原因连不上继续调用原方法的解决方案

《使用@Cacheable注解Redis时Redis宕机或其他原因连不上继续调用原方法的解决方案》在SpringBoot应用中,我们经常使用​​@Cacheable​​注解来缓存数据,以提高应用的性能... 目录@Cacheable注解Redis时,Redis宕机或其他原因连不上,继续调用原方法的解决方案1

sql语句字段截取方法

《sql语句字段截取方法》在MySQL中,使用SUBSTRING函数可以实现字段截取,下面给大家分享sql语句字段截取方法,感兴趣的朋友一起看看吧... 目录sql语句字段截取sql 截取表中指定字段sql语句字段截取1、在mysql中,使用SUBSTRING函数可以实现字段截取。例如,要截取一个字符串字

JAVA数组中五种常见排序方法整理汇总

《JAVA数组中五种常见排序方法整理汇总》本文给大家分享五种常用的Java数组排序方法整理,每种方法结合示例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录前言:法一:Arrays.sort()法二:冒泡排序法三:选择排序法四:反转排序法五:直接插入排序前言:几种常用的Java数组排序

Python将字符串转换为小写字母的几种常用方法

《Python将字符串转换为小写字母的几种常用方法》:本文主要介绍Python中将字符串大写字母转小写的四种方法:lower()方法简洁高效,手动ASCII转换灵活可控,str.translate... 目录一、使用内置方法 lower()(最简单)二、手动遍历 + ASCII 码转换三、使用 str.tr

Python处理超大规模数据的4大方法详解

《Python处理超大规模数据的4大方法详解》在数据的奇妙世界里,数据量就像滚雪球一样,越变越大,从最初的GB级别的小数据堆,逐渐演变成TB级别的数据大山,所以本文我们就来看看Python处理... 目录1. Mars:数据处理界的 “变形金刚”2. Dask:分布式计算的 “指挥家”3. CuPy:GPU

Java中的StringUtils.isBlank()方法解读

《Java中的StringUtils.isBlank()方法解读》:本文主要介绍Java中的StringUtils.isBlank()方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录所在库及依赖引入方法签名方法功能示例代码代码解释与其他方法的对比总结StringUtils.isBl

CentOS7增加Swap空间的两种方法

《CentOS7增加Swap空间的两种方法》当服务器物理内存不足时,增加Swap空间可以作为虚拟内存使用,帮助系统处理内存压力,本文给大家介绍了CentOS7增加Swap空间的两种方法:创建新的Swa... 目录在Centos 7上增加Swap空间的方法方法一:创建新的Swap文件(推荐)方法二:调整Sww

QT6中绘制UI的两种方法详解与示例代码

《QT6中绘制UI的两种方法详解与示例代码》Qt6提供了两种主要的UI绘制技术:​​QML(QtMeta-ObjectLanguage)​​和​​C++Widgets​​,这两种技术各有优势,适用于不... 目录一、QML 技术详解1.1 QML 简介1.2 QML 的核心概念1.3 QML 示例:简单按钮

Oracle 通过 ROWID 批量更新表的方法

《Oracle通过ROWID批量更新表的方法》在Oracle数据库中,使用ROWID进行批量更新是一种高效的更新方法,因为它直接定位到物理行位置,避免了通过索引查找的开销,下面给大家介绍Orac... 目录oracle 通过 ROWID 批量更新表ROWID 基本概念性能优化建议性能UoTrFPH优化建议注