C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ List front()

C++ List front()

C++ List front()函数返回列表的第一个元素。它提供了元素的直接引用。

front()和begin()之间的区别

begin()函数在front处将迭代器返回到元素()函数返回对相同元素的直接引用。

语法

reference front();

参数

它不包含任何参数。

返回值

它返回直接引用

示例1

让我们看一个简单的示例,该列表包含整数值。
#include <iostream>
#include<list>
using namespace std;
int main()
{
    std::list<int> li={1,2,3,4,5};
    std::cout <<"front() is :"<< li.front() << std::endl;
    return 0;
}
输出:
front() is : 1
在此示例中,front()函数返回列表的第一个元素,即1、

示例2

让我们看一个简单的示例,当包含字符值的列表。
#include <iostream>
#include<list>
using namespace std;
int main()
{
    std::list<char> li={'j','a','v','a'};
    std::cout <<"front() is :"<< li.front() << std::endl;
    return 0;
}
输出:
front() is : j
在此示例中,front()函数返回列表的第一个字符,即j。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4