STL标准库(三)之forward_list

如下一个程序进行演示讲解

#include <forward_list> 需要包含该头文件
template <typename T>

void print(T Begin, T end)

{

for (T i = Begin; i != end; ++i)

{

std::cout << *i << std::endl;

}

std::cout << std::endl;

}

int main()

{

std::forward_list<int> obj(5); 声明一个当前有五个元素的单向链表

int temp = 0;

for (auto i = obj.begin(); i != obj.end(); i++)

{

obj.push_front(temp); 相当于头插法

temp++;

}

for (auto i = obj.begin(); i != obj.end(); i++)

{

std::cout << *i << std::endl; 遍历打印

}

struct std::forward_iterator_tag 该迭代器是一个单向迭代器,只能从头部开始使用,支持++ * = 运算符

auto it = obj.begin(); 接受头部迭代器

*it = 123;赋值123

it++; 指向下一个迭代器

*it = 124; 赋值124

obj.pop_front(); 头部元素弹出

obj.clear(); 清空该链表所有元素

print(obj.begin(), obj.end());

system("pause");

return 0;

}

相关推荐
博笙困了3 小时前
AcWing学习——双指针算法
c++·算法
感哥3 小时前
C++ 指针和引用
c++
感哥14 小时前
C++ 多态
c++
沐怡旸21 小时前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
River4161 天前
Javer 学 c++(十三):引用篇
c++·后端
感哥1 天前
C++ std::set
c++
侃侃_天下1 天前
最终的信号类
开发语言·c++·算法
博笙困了1 天前
AcWing学习——差分
c++·算法
echoarts1 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix1 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式