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