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>::clear

void clear();

(C++11 前)

void clear() noexcept;

(C++11 起)

从容器擦除所有元素。此调用后 size() 返回零。

非法化任何指代所含元素的引用、指针或迭代器。任何尾后迭代器保持合法。

参数

(无)

返回值

(无)

复杂度

与容器大小,即元素数成线性。

调用示例

#include <list>
#include <iostream>

int main()
{
    std::list<int> numbers;
    std::cout << "Initially, numbers.empty(): " << numbers.empty() << '\n';

    numbers.push_back(42);
    numbers.push_back(13317);
    std::cout << "After adding elements, numbers.empty(): " << numbers.empty() << '\n';

    numbers.clear();
    std::cout << "clear, numbers.empty(): " << numbers.empty() << '\n';
}

 输出

 

擦除元素

std::list<T,Allocator>::erase

iterator erase( iterator pos );

(1)(C++11 前)

iterator erase( const_iterator pos );

(C++11 起)

iterator erase( iterator first, iterator last );

(2)(C++11 前)

iterator erase( const_iterator first, const_iterator last );

(C++11 起)

从容器擦除指定的元素。

1) 移除位于 pos 的元素。

2) 移除范围 [first; last) 中的元素。

指向被擦除元素的迭代器和引用被非法化。其他引用和迭代器不受影响。

迭代器 pos 必须合法且可解引用。从而不能以 end() 迭代器(合法,但不可解引用)为 pos 的值。

first==last 则迭代器 first 不必可解引用:擦除空范围是无操作。

参数

pos-指向要移除的元素的迭代器
first, last-要移除的元素范围

返回值

后随最后被移除元素的迭代器。若迭代器 pos 指代最后元素,则返回 end() 迭代器。

异常

(无)

复杂度

1) 常数。

2) 与 firstlast 间的距离成线性。

调用示例

#include <list>
#include <iostream>
#include <iterator>

int main()
{
    std::list<int> c{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    for (auto &i : c)
    {
        std::cout << i << " ";
    }
    std::cout << '\n';

    c.erase(c.begin());

    for (auto &i : c)
    {
        std::cout << i << " ";
    }
    std::cout << '\n';

    std::list<int>::iterator range_begin = c.begin();
    std::list<int>::iterator range_end = c.begin();
    std::advance(range_begin, 2);
    std::advance(range_end, 5);

    c.erase(range_begin, range_end);

    for (auto &i : c)
    {
        std::cout << i << " ";
    }
    std::cout << '\n';
}

 输出

访问第一个元素

std::list<T,Allocator>::front

reference front();

const_reference front() const;

返回到容器首元素的引用。

在空容器上对 front 的调用是未定义的。

参数

(无)

返回值

到首元素的引用

复杂度

常数

注意

对于容器 c ,表达式 c.front() 等价于 *c.begin() 。

 调用示例

#include <list>
#include <iostream>

int main()
{
    std::list<char> letters {'o', 'm', 'g', 'w', 't', 'f'};

    if (!letters.empty())
    {
        std::cout << "The first character is: " << letters.front() << '\n';
    }
}

访问最后一个元素

std::list<T,Allocator>::back

reference back();

const_reference back() const;

返回到容器中最后一个元素的引用。

在空容器上对 back 的调用是未定义的。

参数

(无)

返回值

到最后元素的引用。

复杂度

常数。

注意

对于容器 c ,表达式 return c.back(); 等价于 { auto tmp = c.end(); --tmp; return *tmp; }

调用示例

#include <list>
#include <iostream>

int main()
{
    std::list<char> letters {'o', 'm', 'g', 'w', 't', 'f'};
    if (!letters.empty())
    {
        std::cout << "The last character is: " << letters.back() << '\n';
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值