C++ STL中的向量rBoSE()和Read()函数

  1. 向量::rbegin() 是C++ STL中的一个内置函数,它返回指向容器中最后一个元素的反向迭代器。 语法:
vector_name.rbegin()
  1. 参数: 该函数不接受任何参数。 返回值: 该函数返回指向容器中最后一个元素的反向迭代器。 用于演示vector::rbegin()方法的程序: 项目1:

CPP

null

// CPP program to illustrate
// the vector::rbegin() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< int > v;
v.push_back(11);
v.push_back(12);
v.push_back(13);
v.push_back(14);
v.push_back(15);
// prints all the elements
cout << "The vector elements in reverse order are:";
for ( auto it = v.rbegin(); it != v.rend(); it++)
cout << *it << " ";
return 0;
}


输出:

The vector elements in reverse order are:15 14 13 12 11

  1. 向量::rend() 在C++ STL中是一个内置函数,它返回指向向量容器中第一个元素之前的理论元素的反向迭代器。 语法:
vector_name.rend()
  1. 参数: 该函数不接受任何参数。 返回值: 该函数返回一个反向迭代器,指向向量容器中第一个元素之前的理论元素。 用于演示vector::rend()方法的程序: 项目1:

CPP

// CPP program to illustrate
// the vector::rend() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< int > v;
v.push_back(11);
v.push_back(12);
v.push_back(13);
v.push_back(14);
v.push_back(15);
cout << "The last element is: " << *v.rbegin();
// prints all the elements
cout << "The vector elements in reverse order are:";
for ( auto it = v.rbegin(); it != v.rend(); it++)
cout << *it << " ";
return 0;
}


输出:

The last element is: 15The vector elements in reverse order are:15 14 13 12 11

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享