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

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

namespace pmr { template <class T>

using list = std::list<T, std::pmr::polymorphic_allocator<T>>;}

(2)(C++17 起)

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

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

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

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

std::list<T,Allocator>::begin,
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() 。

 参数

(无)

返回值

指向首元素的迭代器。

复杂度

常数。

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

std::list<T,Allocator>::end, 
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 起)

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

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

参数

(无)

返回值

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

复杂度

常数。

 

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

std::list<T,Allocator>::rbegin, 
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 起)

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

 

 参数

(无)

返回值

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

复杂度

常数。

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

std::list<T,Allocator>::rend,
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 起)

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

 

参数

(无)

返回值

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

复杂度

常数。

 

调用示例

#include <iostream>
#include <list>

template<typename T>
std::ostream& operator<<(std::ostream& s, const std::list<T>& v)
{
    s.put('[');
    for (const auto& e : v)
    {
        s << e << " ";
    }
    return s << ']';
}

int main()
{
    std::list<char> strings {'a', 'b', 'c', 'd', 'e', 'f'};
    std::cout << "original strings:                     " << strings << std::endl;

    //iterator 可修改容器元素
    for (std::list<char>::iterator it = strings.begin(); it != strings.end(); it++)
    {
        *it += 6;
    }

    //iterator 遍历元素,打印输出
    std::cout << "iterator *it += 6 strings:            [";
    for (std::list<char>::iterator it = strings.begin(); it != strings.end(); it++)
    {
        std::cout << *it << " ";
    }
    std::cout << "]" << std::endl;


    //const_iterator 不可修改容器元素
//    for (std::list<char>::const_iterator it = strings.cbegin(); it != strings.cend(); it++)
//    {
//        // 编译失败
//        *it += 6;
//    }

    //const_iterator 遍历元素,打印输出
    std::cout << "const_iterator  strings:              [";
    for (std::list<char>::const_iterator it = strings.cbegin(); it != strings.cend(); it++)
    {
        std::cout << *it << " ";
    }
    std::cout << "]" << std::endl;


    //reverse_iterator 可修改容器元素
    for (std::list<char>::reverse_iterator it = strings.rbegin(); it != strings.rend(); it++)
    {
        *it += 6;
    }

    //reverse_iterator 逆向遍历元素,打印输出
    std::cout << "reverse_iterator *it += 6 strings:    [";
    for (std::list<char>::reverse_iterator it = strings.rbegin(); it != strings.rend(); it++)
    {
        std::cout << *it << " ";
    }
    std::cout << "]" << std::endl;

    //const_reverse_iterator 不可修改容器元素
//    for (std::list<char>::const_reverse_iterator it = strings.crbegin(); it != strings.crend(); it++)
//    {
//        // 编译失败
//        *it += 6;
//    }
}

输出

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值