C++ STL 中的 forward_list::begin() 和 forward_list::end()
本文将讨论 C++ 中 forward_list::begin() 和 forward_list::end() 函数的工作原理、语法和示例。
什么是 STL 中的 Forward_list?
Forward list 是一种序列容器,允许在序列中的任何位置进行常数时间插入和删除操作。Forward list 以单链表的形式实现。顺序通过每个元素关联到序列中下一个元素的链接来维护。
什么是 forward_list::begin()?
forward_list::begin() 是 C++ STL 中一个内置函数,在头文件中声明。begin() 返回一个迭代器,该迭代器指向 forward_list 容器中的第一个元素。大多数情况下,我们将 begin() 和 end() 结合使用以提供 forward_list 容器的范围。
语法
forwardlist_container.begin();
此函数不接受任何参数。
返回值
此函数返回一个指向容器第一个元素的双向迭代器。
示例
#include <bits/stdc++.h>
using namespace std;
int main(){
//creating a forward list
forward_list<int> forwardList = { 4, 1, 2, 7 };
cout<<"Printing the elements of a forward List\n";
//calling begin() to point to the first element
for (auto i = forwardList.begin(); i != forwardList.end(); ++i)
cout << ' ' << *i;
return 0;
}输出
如果我们运行上述代码,它将生成以下输出
Printing the elements of a forward List 4 1 2 7
什么是 forward_list::end()?
forward_list::end() 是 C++ STL 中一个内置函数,在头文件中声明。end() 返回一个迭代器,该迭代器指向 forward_list 容器中的最后一个元素。大多数情况下,我们将 begin() 和 end() 结合使用以提供 forward_list 容器的范围。
语法
forwardlist_container.end();
此函数不接受任何参数。
返回值
此函数返回一个指向容器第一个元素的双向迭代器。
示例
#include <bits/stdc++.h>
using namespace std;
int main(){
//creating a forward list
forward_list<int> forwardList = { 4, 1, 2, 7 };
cout<<"Printing the elements of a forward List\n";
//calling begin() to point to the first element
for (auto i = forwardList.begin(); i != forwardList.end(); ++i)
cout << ' ' << *i;
return 0;
}输出
如果我们运行上述代码,它将生成以下输出
Printing the elements of a forward List 4 1 2 7
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP