在C ++ STL中列出begin()和end()
给出的任务是显示STL中C++中的功能列表begin()和列表end()函数。
什么是STL中的列表
列表是一种数据结构,允许按时间顺序在任意位置进行插入和删除。列表被实现为双向链接列表。列表允许非连续的内存分配。与数组,向量和双端队列相比,列表在容器中的任何位置执行元素的插入提取和移动效果更好。在列表中,对元素的直接访问很慢,并且列表与forward_list相似,但是转发列表对象是单个链接列表,并且只能迭代转发。
什么是begin()
列表begin()用于返回指向列表的第一个元素的迭代器。
语法
list_name.begin( )
什么是end()?
列表end()用于返回指向列表最后一个元素的迭代器。
语法
list_name.end( )
示例
输出 - 列表-1011121314
输出 - 列表-6667686970
可以遵循的方法
首先我们初始化列表
然后定义begin()和end()。
通过使用上述方法,我们可以使用begin()和end()函数打印列表。
示例
/ / C++ code to demonstrate the working of begin( ) and end( ) function in STL #include <iostream.h> #include<list.h> Using namespace std; int main ( ){ List<int> list = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }; / / print the list cout<< “ Elements in List: “; for( auto x = list.begin( ); x != list.end( ); ++x) cout<> *x << “ “; return 0; }
输出结果
如果我们运行以上代码,它将生成以下输出
Elements of List: 11 12 13 14 15 16 17 18 19 20
示例
/ / C++ code to demonstrate the working of list begin( ) and end( ) function in STL #include<iostream.h> #include<list.h> Using namespace std; int main ( ){ List list = { ‘D’, ‘E’, ‘S’, ‘I’, ‘G’, ‘N’ }; / / print the list cout << “ Elements in List: “; for( auto x = list.begin( ); x != list.end( ); ++x) cout<< *x << “ “; return 0; }
输出结果
如果我们运行以上代码,它将生成以下输出
Elements in List: D E S I G N