• c++标准模板(STL)(std::list)(三)


    定义于头文件 
    template<  class T,  class Allocator = std::allocator > class list;(1)

    namespace pmr { template

    using list = std::list>;}

    (2)(C++17 起)

    std::list 是支持常数时间从容器任何位置插入和移除元素的容器。不支持快速随机访问。它通常实现为双向链表。与 std::forward_list 相比,此容器提供双向迭代但在空间上效率稍低。

    在 list 内或在数个 list 间添加、移除和移动元素不会非法化迭代器或引用。迭代器仅在对应元素被删除时非法化。

    std::list 满足容器 (Container) 、具分配器容器 (AllocatorAwareContainer) 、序列容器 (SequenceContainer) 及可逆容器 (ReversibleContainer) 的要求。

    返回指向容器第一个元素的迭代器

    1. std::list<T,Allocator>::begin,
    2. std::list<T,Allocator>::cbegin

    iterator begin();

    (C++11 前)

    iterator begin() noexcept;

    (C++11 起)

    const_iterator begin() const;

    (C++11 前)

    const_iterator begin() const noexcept;

    (C++11 起)

    const_iterator cbegin() const noexcept;

    (C++11 起)

    返回指向容器首元素的迭代器。

    若容器为空,则返回的迭代器将等于 end() 。

     参数

    (无)

    返回值

    指向首元素的迭代器。

    复杂度

    常数。

    返回指向容器尾端的迭代器

    1. std::list<T,Allocator>::end,
    2. std::list<T,Allocator>::cend

    iterator end();

    (C++11 前)

    iterator end() noexcept;

    (C++11 起)

    const_iterator end() const;

    (C++11 前)

    const_iterator end() const noexcept;

    (C++11 起)

    const_iterator cend() const noexcept;

    (C++11 起)

    返回指向容器末元素后一元素的迭代器。

    此元素表现为占位符;试图访问它导致未定义行为。

    参数

    (无)

    返回值

    指向后随最后元素的迭代器。

    复杂度

    常数。

     

    返回指向容器最后元素的逆向迭代器

    1. std::list<T,Allocator>::rbegin,
    2. std::list<T,Allocator>::crbegin

    reverse_iterator rbegin();

    (C++11 前)

    reverse_iterator rbegin() noexcept;

    (C++11 起)

    const_reverse_iterator rbegin() const;

    (C++11 前)

    const_reverse_iterator rbegin() const noexcept;

    (C++11 起)

    const_reverse_iterator crbegin() const noexcept;

    (C++11 起)

    返回指向逆向容器首元素的逆向迭代器。它对应非逆向容器的末元素。

     

     参数

    (无)

    返回值

    指向首元素的逆向迭代器。

    复杂度

    常数。

    返回指向前端的逆向迭代器

    1. std::list<T,Allocator>::rend,
    2. std::list<T,Allocator>::crend

    reverse_iterator rend();

    (C++11 前)

    reverse_iterator rend() noexcept;

    (C++11 起)

    const_reverse_iterator rend() const;

    (C++11 前)

    const_reverse_iterator rend() const noexcept;

    (C++11 起)

    const_reverse_iterator crend() const noexcept;

    (C++11 起)

    返回指向逆向容器末元素后一元素的逆向迭代器。它对应非逆向容器首元素的前一元素。此元素表现为占位符,试图访问它导致未定义行为。

     

    参数

    (无)

    返回值

    指向末元素后一元素的逆向迭代器。

    复杂度

    常数。

     

    调用示例

    1. #include <iostream>
    2. #include <list>
    3. template<typename T>
    4. std::ostream& operator<<(std::ostream& s, const std::list<T>& v)
    5. {
    6. s.put('[');
    7. for (const auto& e : v)
    8. {
    9. s << e << " ";
    10. }
    11. return s << ']';
    12. }
    13. int main()
    14. {
    15. std::list<char> strings {'a', 'b', 'c', 'd', 'e', 'f'};
    16. std::cout << "original strings: " << strings << std::endl;
    17. //iterator 可修改容器元素
    18. for (std::list<char>::iterator it = strings.begin(); it != strings.end(); it++)
    19. {
    20. *it += 6;
    21. }
    22. //iterator 遍历元素,打印输出
    23. std::cout << "iterator *it += 6 strings: [";
    24. for (std::list<char>::iterator it = strings.begin(); it != strings.end(); it++)
    25. {
    26. std::cout << *it << " ";
    27. }
    28. std::cout << "]" << std::endl;
    29. //const_iterator 不可修改容器元素
    30. // for (std::list<char>::const_iterator it = strings.cbegin(); it != strings.cend(); it++)
    31. // {
    32. // // 编译失败
    33. // *it += 6;
    34. // }
    35. //const_iterator 遍历元素,打印输出
    36. std::cout << "const_iterator strings: [";
    37. for (std::list<char>::const_iterator it = strings.cbegin(); it != strings.cend(); it++)
    38. {
    39. std::cout << *it << " ";
    40. }
    41. std::cout << "]" << std::endl;
    42. //reverse_iterator 可修改容器元素
    43. for (std::list<char>::reverse_iterator it = strings.rbegin(); it != strings.rend(); it++)
    44. {
    45. *it += 6;
    46. }
    47. //reverse_iterator 逆向遍历元素,打印输出
    48. std::cout << "reverse_iterator *it += 6 strings: [";
    49. for (std::list<char>::reverse_iterator it = strings.rbegin(); it != strings.rend(); it++)
    50. {
    51. std::cout << *it << " ";
    52. }
    53. std::cout << "]" << std::endl;
    54. //const_reverse_iterator 不可修改容器元素
    55. // for (std::list<char>::const_reverse_iterator it = strings.crbegin(); it != strings.crend(); it++)
    56. // {
    57. // // 编译失败
    58. // *it += 6;
    59. // }
    60. }

    输出

     

  • 相关阅读:
    BaaS、FaaS、Serverless 都是什么?
    基于邻接矩阵的克鲁斯卡尔算法和普利姆算法
    centos7下部署oracle 11g
    基于Spring Boot的职业生涯规划系统开题报告
    前缀和实例1 (【模板】前缀和 )
    目标检测算法
    三元组(C++ 实现矩阵快速转置)
    JVM类加载机制
    Android 包体积优化(未完成)
    【数据结构】排序详解二
  • 原文地址:https://blog.csdn.net/qq_40788199/article/details/127034085