博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
forward_list学习之删除操作clear, erase_after,pop_front,max_size
阅读量:2193 次
发布时间:2019-05-02

本文共 1721 字,大约阅读时间需要 5 分钟。

本篇学习forward_list删除操作

clear:清除内容

erase_after:擦除元素后的元素

pop_front:移除首元素

max_size:返回可容纳的最大元素数

代码实现:

#include 
#include
#include
#include
using namespace std;void deleteElement(){ //1.clear删除内容 forward_list
list1 = {22, 25, 27, 30, 33, 36, 39, 41, 42}; cout << "list1 的值: "; for(auto &val: list1) cout << val << "\t"; cout << endl; //1.erase_after擦除pos元素之后的值,并返擦除的元素之后的迭代器 auto it1 = list1.erase_after(list1.before_begin());//擦除首元素,即删除22,返回25 cout << "it1 = " << *it1 << endl; cout << "list1 的值: "; for(auto &val: list1) cout << val << "\t"; cout << endl; //擦除第2个元素,返回第三个元素的迭代器,即删除27返回30 auto it2 = list1.erase_after(list1.begin()); cout << "it2 = " << *it2 << endl; cout << "list1 的值: "; for(auto &val: list1) cout << val << "\t"; cout << endl; //范围删除 25 30 33 36 39 41 42 auto start = std::next(list1.begin());//第2个位置开始 30 auto end = std::next(start, 4);//终点 41 cout << "start = " << *start << " end = " << *end << endl; //删除范围(start, end)不包括start, end两个点 list1.erase_after(start, end); cout << "list1 的值: "; for(auto &val: list1) cout << val << "\t"; //25 30 41 42 cout << endl; //3.pop_front:移除首元素 list1.pop_front();//无返回值 cout << "list1 的值: "; for(auto &val: list1) cout << val << "\t"; //30 41 42 cout << endl; //4.max_size:返回可容纳的最大元素数 cout << "list1.max_size = " << list1.max_size() << endl; cout << endl;}int main(){ deleteElement(); cout << endl; cout << "Hello World!" << endl; return 0;}

运行结果:

clear是删除所有肉容,而erase_after是擦除一个值或一个范围里的值。

参考

 

转载地址:http://kbiub.baihongyu.com/

你可能感兴趣的文章
Java多线程学习
查看>>
检查Linux服务器性能
查看>>
Java 8新的时间日期库
查看>>
Chrome开发者工具
查看>>
【LEETCODE】102-Binary Tree Level Order Traversal
查看>>
【LEETCODE】106-Construct Binary Tree from Inorder and Postorder Traversal
查看>>
【LEETCODE】202-Happy Number
查看>>
和机器学习和计算机视觉相关的数学
查看>>
十个值得一试的开源深度学习框架
查看>>
【LEETCODE】240-Search a 2D Matrix II
查看>>
【LEETCODE】53-Maximum Subarray
查看>>
【LEETCODE】215-Kth Largest Element in an Array
查看>>
【LEETCODE】241-Different Ways to Add Parentheses
查看>>
【LEETCODE】312-Burst Balloons
查看>>
【LEETCODE】232-Implement Queue using Stacks
查看>>
【LEETCODE】225-Implement Stack using Queues
查看>>
【LEETCODE】155-Min Stack
查看>>
【LEETCODE】20-Valid Parentheses
查看>>
【LEETCODE】290-Word Pattern
查看>>
【LEETCODE】36-Valid Sudoku
查看>>