C++ STL中的列表结束()函数

这个 列表::end() 是C++中的一个内置函数,用来获取迭代器来传递最后一个元素。超过最后一个元素意味着end()函数返回的迭代器将迭代器返回到列表容器中最后一个元素后面的元素。它不能用于修改元素或列表容器。

null

此函数基本上与list::begin()函数一起用于设置范围。

语法:

list_name.end() 

参数: 这个函数不接受任何参数,它只是返回一个迭代器,使其超过最后一个元素。

返回值: 此函数返回一个迭代器,使元素超过列表的最后一个元素。

下面的程序演示了list::end()函数。

// CPP program to illustrate the
// list::end() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Creating a list
list< int > demoList;
// Add elements to the List
demoList.push_back(10);
demoList.push_back(20);
demoList.push_back(30);
demoList.push_back(40);
// using end() to get iterator
// to past the last element
list< int >::iterator it = demoList.end();
// This will not print the last element
cout << "Returned iterator points to : " << *it << endl;
// Using end() with begin() as a range to
// print all of the list elements
for ( auto itr = demoList.begin();
itr != demoList.end(); itr++) {
cout << *itr << " " ;
}
return 0;
}


输出:

Returned iterator points to : 4
10 20 30 40

笔记 :此函数在恒定的时间复杂度下工作。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享