序列型容器之forward_list---单向列表

https://en.cppreference.com/w/cpp/container

  • 头文件

<forward_list>

  • 声明

1) (since C++11)

template<
    class T,
    class Allocator = std::allocator<T>
> class forward_list;

2) (since C++17)

namespace pmr {
    template <class T>
    using forward_list = std::forward_list<T, std::pmr::polymorphic_allocator<T>>;
}

  • 描述

std::forward_list是一个能够支持快速插入和删除元素的容器。它不支持随机访问。它是一个单向链表的实现,与c的实现相比,它没有额外的开销。与std::list 相比,当不需要双向迭代时,它节省更多的空间。

此外,删除或移动链表中的元素不能使得指向其它元素的迭代器非法。然而,当对应的元素从链表中删除时,指向这个元素的迭代器或引用都会是非法的。

std::forward_list meets the requirements of Container (except for the size member function and that operator=='s complexity is always linear), AllocatorAwareContainer and SequenceContainer.

  • 模板参数
T

the type of the elements.
The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type is a complete type and meets the requirements of Erasable, but many member functions impose stricter requirements.    (until C++17)

The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type meets the requirements of Erasable, but many member functions impose stricter requirements. This container (but not its members) can be instantiated with an incomplete element type if the allocator satisfies the allocator completeness requirements.    (since C++17)

AllocatorAn allocator that is used to acquire/release memory and to construct/destroy the elements in that memory. The type must meet the requirements of Allocator. The behavior is undefined if Allocator::value_type is not the same as T.
  • 成员类型
成员类型定义
value_typeT
allocator_typeAllocator
size_typeUnsigned integer type (usually std::size_t)
difference_type Signed integer type (usually std::ptrdiff_t)
referencevalue_type&
const_referenceconst value_type&
pointerstd::allocator_traits<Allocator>::pointer
const_pointer std::allocator_traits<Allocator>::const_pointer
iterator LegacyForwardIterator
const_iteratorConstant LegacyForwardIterator
  • 元素访问
frontaccess the first element
(public member function)
  • 迭代器
before_begin
cbefore_begin
returns an iterator to the element before beginning
(public member function)
begin
cbegin
returns an iterator to the beginning
(public member function)
end
cend
returns an iterator to the end
(public member function)
  • 指标
emptychecks whether the container is empty
(public member function)
max_sizereturns the maximum possible number of elements
(public member function)
  • 修改
clearvoid clear() noexcept;(since C++11)clears the contents
(public member function)
insert_after(1)    (since C++11)iterator insert_after( const_iterator pos, const T& value );
(2)    (since C++11)iterator insert_after( const_iterator pos, T&& value );
(3)    (since C++11)iterator insert_after( const_iterator pos, size_type count, const T& value );
(4)    (since C++11)template< class InputIt >iterator insert_after( const_iterator pos, InputIt first, InputIt last );
(5)    (since C++11)iterator insert_after( const_iterator pos, std::initializer_list<T> ilist );
inserts elements after an element
(public member function)
emplace_aftertemplate< class... Args >iterator emplace_after( const_iterator pos, Args&&... args );
(since C++11)
constructs elements in-place after an element
(public member function)
erase_after(1)    (since C++11)iterator erase_after( const_iterator pos );
(2)    (since C++11)iterator erase_after( const_iterator first, const_iterator last );
erases an element after an element
(public member function)
push_front(since C++11)void push_front( const T& value );
(since C++11)void push_front( T&& value );
inserts an element to the beginning
(public member function)
emplace_front(since C++11)(until C++17)template< class... Args >void emplace_front( Args&&... args );
(since C++17)template< class... Args >
reference emplace_front( Args&&... args );
constructs an element in-place at the beginning
(public member function)
pop_frontvoid pop_front();
(since C++11)
removes the first element
(public member function)
resize(1)    void resize( size_type count );
(2)    void resize( size_type count, const value_type& value );
changes the number of elements stored
(public member function)
swap(since C++11)(until C++17)void swap( forward_list& other );
(since C++17)void swap( forward_list& other ) noexcept(/* see below */);
swaps the contents
(public member function)
  • 操作
merge

(1)  

 (since C++11)

void merge( forward_list& other );
(since C++11)void merge( forward_list&& other );
(2)   
(since C++11)template <class Compare>
void merge( forward_list& other, Compare comp );
(since C++11)template <class Compare>void merge( forward_list&& other, Compare comp );

merges two sorted lists
(public member function)
splice_after

(1)

(since C++11)void splice_after( const_iterator pos, forward_list& other );
(since C++11)void splice_after( const_iterator pos, forward_list&& other );
(2)    
 (since C++11)void splice_after( const_iterator pos, forward_list& other,const_iterator it );
  (since C++11)void splice_after( const_iterator pos, forward_list&& other,const_iterator it );
(3)    
(since C++11)void splice_after(const_iterator pos, forward_list& other, const_iterator first, const_iterator last );
(since C++11)void splice_after( const_iterator pos, forward_list&& other,
   const_iterator first, const_iterator last );

moves elements from another forward_list
(public member function)
remove
remove_if
(since C++11)(until C++20)void remove( const T& value );
(since C++20)size_type remove( const T& value );
(since C++11)(until C++20)template< class UnaryPredicate >void remove_if( UnaryPredicate p );
(since C++20)template< class UnaryPredicate >size_type remove_if( UnaryPredicate p );
removes elements satisfying specific criteria
(public member function)
reversevoid reverse() noexcept;
(since C++11)
removes elements satisfying specific criteria
(public member function)
unique(1)    
(since C++11)(until C++20)void unique();
(since C++20)size_type unique();
(2)    
(since C++11)(until C++20)template< class BinaryPredicate >void unique( BinaryPredicate p );
(since C++20)template< class BinaryPredicate >size_type unique( BinaryPredicate p );
removes consecutive duplicate elements
(public member function)
sort(1)    (since C++11)void sort();
(2)    (since C++11)template< class Compare >void sort( Compare comp );
sorts the elements
(public member function)
  • 举例

#include <iostream>
#include <forward_list>

int main()
{
    // Create a list containing integers
    std::forward_list<int> l = { 7, 5, 16, 8 , 1, 2, 16, 3,4, 6, 16};
    // Add an integer to the front of the list
    l.push_front(25);
    // Insert an integer after 16 by searching
    auto it = std::find(l.begin(), l.end(), 16);
    while(it != l.end()) {
        it = l.insert_after(it, 42);
        std::cout<<" :"<<*it<<std::endl;
        it = std::find(it, l.end(), 16);
    }
    // Iterate and print values of the list
    for (int n : l) {
        std::cout << n << '\n';
    }
    std::cout << "*****************************"<< '\n';
    //erase first 16
    it = l.begin();
    auto prevIt = it;
    while(it != l.end()) {
        if(*it == 16){       
            if(it == l.begin()){
                l.pop_front();
                it = l.begin();
            }
            else{
                it = l.erase_after(prevIt);
            }
        }
        else{
            prevIt = it;
            it++;
        }
    }
    // Iterate and print values of the list
    for (int n : l) {
        std::cout << n << '\n';
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值