C++ 向量 中的 crend()函数

首页 / C++入门教程 / C++ 向量 中的 crend()函数

此函数用于指向向量集合中第一个元素之前的元素。它指的是反向端。

crend - 语法

const_reverse_iterator itr=v.crend();

crend - 返回值

它返回常数反向迭代器,该迭代器指向序列中的反向端。

无涯教程网

crend - 例子1

让我们看一个简单的例子。

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-vector-crend-function.html

来源:LearnFk无涯教程网

#include <iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v{1,2,3,4,5};
vector<int>::const_reverse_iterator itr=v.crend()-2;
  *itr=9;
cout<<*itr;
return 0;
}
#In this example, it shows that crend() function does not modify the value otherwise, it shows an error.

crend - 例子2

我们来看另一个简单的例子

#include <iostream>
#include<vector>
using namespace std;
int main()
{
vector<string>str{"java","C","C++",".Net"};
vector<string>::const_reverse_iterator itr=str.crend()-1;
std::cout<< *itr;
return 0;
}

输出:

java

在此示例中,crend()函数访问向量集合的第一个元素。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

微服务架构实战160讲 -〔杨波〕

从0开始学游戏开发 -〔蔡能〕

Go语言从入门到实战 -〔蔡超〕

Node.js开发实战 -〔杨浩〕

RPC实战与核心原理 -〔何小锋〕

检索技术核心20讲 -〔陈东〕

正则表达式入门课 -〔涂伟忠〕

张汉东的Rust实战课 -〔张汉东〕

大厂晋升指南 -〔李运华〕

好记忆不如烂笔头。留下您的足迹吧 :)