您的当前位置:首页正文

stl rbegin_vector :: rbegin()函数以及C ++ STL中的示例

2024-11-09 来源:个人技术集锦

stl rbegin

C ++ vector :: rbegin()函数 (C++ vector::rbegin() function)

vector::rbegin() is a library function of "vector" header, it is used to get the last element of a vector using reverse_iterator, it returns a reverse iterator pointing to the last elements (i.e. the reverse beginning) of a vector.

vector :: rbegin()“ vector”头文件的库函数,用于使用reverse_iterator获取向量的最后一个元素,它返回指向向量的最后一个元素(即,反向开始)的反向迭代器。

Note: To use vector, include <vector> header.

注意:要使用向量,请包含<vector>标头。

Syntax of vector::rbegin() function

vector :: rbegin()函数的语法

    vector::rbegin();

Parameter(s): none – It accepts nothing.

参数: 无 –不接受任何内容。

Return value: iterator – It returns a reverse iterator pointing to the last element of the vector.

返回值: iterator –返回一个反向迭代器,指向向量的最后一个元素。

Example:

例:

    Input:
    vector<int> vector1{ 1, 2, 3, 4, 5 };

    Function call:
    vector<int>::reverse_iterator rit;
    rit = vector1.rbegin();
    cout << *rit;

    Output:
    5

C ++程序演示vector :: rbegin()函数的示例 (C++ program to demonstrate example of vector::rbegin() function)

//C++ STL program to demonstrate example of 
//vector::rbegin() function

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v1;

    v1.push_back(10);
    v1.push_back(20);
    v1.push_back(30);
    v1.push_back(40);
    v1.push_back(50);

    //creating iterator
    vector<int>::reverse_iterator rit;
    rit = v1.rbegin();
    cout << "last element is: " << *rit << endl;

    return 0;
}

Output

输出量

last element is: 50

Reference:

参考:

stl rbegin

显示全文