在本文中,我们将讨论C ++中list::end()函数的工作原理,语法和示例。
列表是一种数据结构,可以按时间顺序在任何位置进行恒定的插入和删除。列表被实现为双向链接列表。列表允许非连续的内存分配。与数组,向量和双端队列相比,列表在容器中的任何位置执行元素的插入提取和移动效果更好。在列表中,直接访问该元素的速度很慢,并且列表类似于forward_list,但是转发列表对象是单个链接列表,并且只能迭代转发。
list::end()是C ++ STL中的内置函数,在<list>头文件中声明。end()
返回迭代器,该迭代器被引用到列表容器中终点位置旁边的元素。此函数不指向容器中的任何元素。
此函数通常与list::begin()一起使用,以提供特定列表容器的范围。
list_container.end();
此函数不接受任何参数。
此函数将过去返回到列表容器的end元素。
/ *在下面的代码中,我们使用函数end()
使用给定的函数遍历列表中存在的元素。* /
#include <bits/stdc++.h> using namespace std; int main(){ //创建一个列表 list<int> myList; //使用push_back()函数将元素插入到列表中 myList.push_back(67); myList.push_back(12); myList.push_back(32); myList.push_back(780); myList.push_back(78); cout<<"elements in the list are :\n"; for (auto j = myList.begin(); j!= myList.end(); j++){ cout << *j << " "; } return 0; }
如果我们运行上面的代码,它将生成以下输出
Elements in the list are: 67 12 32 780 78
/ *在下面的代码中,我们使用头文件使用该end()
函数访问列表中存在的元素。* /
#include <iostream> #include <list> int main (){ //创建元素数组 int values[] = {67, 12, 32, 780, 78}; //将值插入列表 std::list<int> myList (values,values+5); std::cout << "elements in the list are :\n"; //accessing list elements using iterator returned by an end() function for (std::list<int>::iterator i = myList.begin() ; i != myList.end(); ++i) std::cout << ' ' << *i; return 0; }
输出结果
如果我们运行上面的代码,它将生成以下输出
Elements in the list are: 67 12 32 780 78