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;

}

相关推荐

  1. STL标准forward_list

    2024-01-27 12:40:03       29 阅读
  2. STL标准)序列容器list

    2024-01-27 12:40:03       34 阅读
  3. c++ STL标准容器

    2024-01-27 12:40:03       30 阅读
  4. Go语言标准log和zap

    2024-01-27 12:40:03       8 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-27 12:40:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-27 12:40:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-27 12:40:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-27 12:40:03       18 阅读

热门阅读

  1. PostgreSQL(十一)coalesce() 函数的用法

    2024-01-27 12:40:03       30 阅读
  2. 构建支持 gpu 的 jupyterlab docker 镜像

    2024-01-27 12:40:03       34 阅读
  3. [go] 享元模式

    2024-01-27 12:40:03       37 阅读