C++ STL 中的 forward_list::remove()
在本文中,我们将讨论 C++ 中 forward_list::remove() 和 forward_list::remove_if() 函数的工作原理、语法和示例。
什么是 STL 中的 Forward_list?
正向列表是序列容器,它允许对序列中的任何位置进行恒定时间插入和删除操作。正向列表以单链表方式实现。通过将每个元素与序列中下一个元素的链接关联起来保持排序。
什么是 forward_list::remove()?
forward_list::remove() 是一项 C++ STL 中的内置函数,在头文件中进行声明。remove() 用于从正向列表中删除所有元素。会根据删除的元素数量减少容器大小。
语法
flist_container1.remove(const value_type& value );
该函数只能接受一个参数,即要插入开头的值。
返回值
此函数不返回任何值
示例
在下面的代码中,我们
#include <forward_list>
#include <iostream>
using namespace std;
int main(){
forward_list<int> forwardList = {2, 3, 1, 1, 1, 6, 7};
//List before applying remove operation
cout<<"list before applying remove operation : ";
for(auto i = forwardList.begin(); i != forwardList.end(); ++i)
cout << ' ' << *i;
//List after applying remove operation
cout<<"\nlist after applying remove operation : ";
forwardList.remove(1);
for(auto i = forwardList.begin(); i != forwardList.end(); ++i)
cout << ' ' << *i;
}输出
如果我们运行上面的代码,它将生成以下输出
list before applying remove operation : 2, 3, 1, 1, 1, 6, 7 list after applying remove operation : 2, 3, 6, 7
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP