stl::list_迭代列表(list :: begin()和list :: end()函数的示例)| C ++ STL

stl::list

Given a list and we have to iterate its all elements and print in new line in C++.

给定一个列表,我们必须对其所有元素进行迭代,并在C ++中以新行打印。

Example:

例:

    Input: list num{10, 20, 30, 40, 50}

    Output:
    List elements are:
    10
    20
    30
    40
    50

列表迭代器 (List iterator)

To iterate a list in C++ STL, we need an iterator that should be initialized with the first element of the list and we need to check it till the end of the list.

要在C ++ STL中迭代列表 ,我们需要一个迭代器,该迭代器应使用列表的第一个元素进行初始化,并且需要对其进行检查,直到列表末尾为止。

List iterator declaration:

列出迭代器声明:

 list
   
   
    
    ::iterator it;
   
   

list :: begin()和list :: end()函数 (list::begin() and list::end() functions)

The function list::begin() returns an iterator pointing to the first element i.e. returns reference to the first element and list::end() returns an iterate pointing to the last element.

函数list :: begin()返回指向第一个元素的迭代器,即返回对第一个元素的引用,而list :: end()返回指向最后一个元素的迭代器。

Syntax:

句法:

    list_name.begin();
    list_name.end();

Program:

程序:

#include <iostream>
#include <list>

using namespace std;

int main() {
	// declare a list
	list<int>num {10, 20, 30, 40, 50};
	
	//declare an interator
	list<int>::iterator it;

	//run loop using begin() end() functons
	cout<< "List elements are: " <<endl;
	for ( it=num.begin (); it!=num.end (); it++)
		cout<< *it <<endl;

	return 0;
	
}

Output

输出量

    List elements are: 
    10
    20
    30
    40
    50


翻译自: https://www.includehelp.com/stl/iterate-a-list-example-of-list-begin-and-list-end-functions.aspx

stl::list

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值