stl vector 函数_vector :: end()函数,以及C ++ STL中的示例

stl vector 函数

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

vector::end() is a library function of "vector" header, it can be used to get the last element of a vector. It returns an iterator pointing to the past-the-end element of the vector.

vector :: end()“ vector”头文件的库函数,可用于获取矢量的最后一个元素。 它返回一个指向向量的past-the-end元素的迭代器。

Note:

注意:

  • To use vector, include <vector> header.

    要使用vector,请包含<vector>标头。

  • It does not point to the last element, thus to get the last element we can use vector::end()-1.

    它不指向最后一个元素,因此要获取最后一个元素,我们可以使用vector :: end()-1 。

Syntax of vector::end() function

vector :: end()函数的语法

    vector::end();

Parameter(s): none – It accepts nothing.

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

Return value: iterator – It returns an iterator pointing to the past-the-end element of the vector.

返回值: iterator –返回一个迭代器,该迭代器指向向量的past-the-end元素。

Example:

例:

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

    Function call:
    vector<int>::iterator it;
    it = vector1.end()-1;
    cout << *it << endl;

    Output:
    5

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

//C++ STL program to demonstrate example of 
//vector::end() 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>::iterator it;
    it = v1.end()-1;
    cout << "last element is: " << *it << endl;

    return 0;
}

Output

输出量

last element is: 50

Reference: C++ vector::end()

参考: C ++ vector :: end()

翻译自: https://www.includehelp.com/stl/vector-end-function-with-example.aspx

stl vector 函数

已标记关键词 清除标记
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页