序列型容器之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)

Allocator An 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_type T
allocator_type Allocator
size_type Unsigned integer type (usually std::size_t)
difference_type  Signed integer type (usually std::ptrdiff_t)
reference value_type&
const_reference const value_type&
pointer std::allocator_traits<Allocator>::pointer
const_pointer  std::allocator_traits<Allocator>::const_pointer
iterator  LegacyForwardIterator
const_iterator Constant LegacyForwardIterator
  • 元素訪問
front access 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)
  • 指標
empty checks whether the container is empty
(public member function)
max_size returns the maximum possible number of elements
(public member function)
  • 修改
clear void 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_after template< 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_front void 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)
reverse void 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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章