| Home | Libraries | People | FAQ | More

## Class template slist

boost::interprocess::slist

## Synopsis

    template<typename T, typename A>
    class slist {
    public:
      // types
      typedef T value_type;
      typedef A::pointer         pointer;                // Pointer to T.
      typedef A::const_pointer   const_pointer;          // Const pointer to T.
      typedef A::reference       reference;              // Reference to T.
      typedef A::const_reference const_reference;        // Const reference to T.
      typedef A::size_type       size_type;              // An unsigned integral type.
      typedef A::difference_type difference_type;        // A signed integral type.
      typedef A                  allocator_type;         // The allocator type.
      typedef NodeAlloc          stored_allocator_type;  // The stored allocator type.

      // construct/copy/destruct
      slist(const allocator_type & = allocator_type());
      slist(size_type, const value_type & = value_type(),
            const allocator_type & = allocator_type());
      template<typename InpIt>
        slist(InpIt, InpIt, const allocator_type & = allocator_type());
      slist(const slist &);
      slist(unspecified);
      slist& operator=(const slist &);
      slist& operator=(unspecified);
      ~slist();

      // public member functions
      allocator_type get_allocator() const;
      const stored_allocator_type & get_stored_allocator() const;
      stored_allocator_type & get_stored_allocator() ;
      void assign(size_type, const T &) ;
      template<typename InpIt> void assign(InpIt, InpIt) ;
      iterator begin() ;
      const_iterator begin() const;
      iterator end() ;
      const_iterator end() const;
      iterator before_begin() ;
      const_iterator before_begin() const;
      size_type size() const;
      size_type max_size() const;
      bool empty() const;
      void swap(slist &) ;
      reference front() ;
      const_reference front() const;
      void push_front(const value_type &) ;
      void push_front(unspecified) ;
      void pop_front() ;
      iterator previous(iterator) ;
      const_iterator previous(const_iterator) ;
      iterator insert_after(iterator, const value_type &) ;
      iterator insert_after(iterator, unspecified) ;
      void insert_after(iterator, size_type, const value_type &) ;
      template<typename InIter> void insert_after(iterator, InIter, InIter) ;
      iterator insert(iterator, const value_type &) ;
      iterator insert(iterator, unspecified) ;
      void insert(iterator, size_type, const value_type &) ;
      template<typename InIter> void insert(iterator, InIter, InIter) ;
      iterator erase_after(iterator) ;
      iterator erase_after(iterator, iterator) ;
      iterator erase(iterator) ;
      iterator erase(iterator, iterator) ;
      void resize(size_type, const T &) ;
      void resize(size_type) ;
      void clear() ;
      void splice_after(iterator, slist &) ;
      void splice_after(iterator, slist &, iterator) ;
      void splice_after(iterator, slist &, iterator, iterator) ;
      void splice_after(iterator, slist &, iterator, iterator, size_type) ;
      void splice(iterator, slist &) ;
      void splice(iterator, slist &, iterator) ;
      void splice(iterator, slist &, iterator, iterator) ;
      void reverse() ;
      void remove(const T &) ;
      template<typename Pred> void remove_if(Pred) ;
      void unique() ;
      template<typename Pred> void unique(Pred) ;
      void merge(slist &) ;
      template<typename StrictWeakOrdering>
        void merge(slist &, StrictWeakOrdering) ;
      void sort() ;
      template<typename StrictWeakOrdering> void sort(StrictWeakOrdering) ;
    };

## Description

An slist is a singly linked list: a list where each element is linked to the next element, but not to the previous element. That is, it is a Sequence that supports forward but not backward traversal, and (amortized) constant time insertion and removal of elements. Slists, like lists, have the important property that insertion and splicing do not invalidate iterators to list elements, and that even removal invalidates only the iterators that point to the elements that are removed. The ordering of iterators may be changed (that is, slist<T>::iterator might have a different predecessor or successor after a list operation than it did before), but the iterators themselves will not be invalidated or made to point to different elements unless that invalidation or mutation is explicit.

The main difference between slist and list is that list's iterators are bidirectional iterators, while slist's iterators are forward iterators. This means that slist is less versatile than list; frequently, however, bidirectional iterators are unnecessary. You should usually use slist unless you actually need the extra functionality of list, because singly linked lists are smaller and faster than double linked lists.

Important performance note: like every other Sequence, slist defines the member functions insert and erase. Using these member functions carelessly, however, can result in disastrously slow programs. The problem is that insert's first argument is an iterator p, and that it inserts the new element(s) before p. This means that insert must find the iterator just before p; this is a constant-time operation for list, since list has bidirectional iterators, but for slist it must find that iterator by traversing the list from the beginning up to p. In other words: insert and erase are slow operations anywhere but near the beginning of the slist.

Slist provides the member functions insert_after and erase_after, which are constant time operations: you should always use insert_after and erase_after whenever possible. If you find that insert_after and erase_after aren't adequate for your needs, and that you often need to use insert and erase in the middle of the list, then you should probably use list instead of slist.

### `slist` public types

1. typedef T value_type;

   The type of object, T, stored in the list

### `slist` public construct/copy/destruct

1.     slist(const allocator_type & a = allocator_type());

   Effects: Constructs a list taking the allocator as parameter.

   Throws: If allocator_type's copy constructor throws.

   Complexity: Constant.

2.     slist(size_type n, const value_type & x = value_type(),
             const allocator_type & a = allocator_type());

   Effects: Constructs a list that will use a copy of allocator a and inserts n copies of value.

   Throws: If allocator_type's default constructor or copy constructor throws or T's default or copy constructor throws.

   Complexity: Linear to n.

3.     template<typename InpIt>
         slist(InpIt first, InpIt last, const allocator_type & a = allocator_type());

   Effects: Constructs a list that will use a copy of allocator a and inserts a copy of the range [first, last) in the list.

   Throws: If allocator_type's default constructor or copy constructor throws or T's constructor taking an dereferenced InIt throws.

   Complexity: Linear to the range [first, last).

4.     slist(const slist & x);

   Effects: Copy constructs a list.

   Postcondition: x == *this.

   Throws: If allocator_type's default constructor or copy constructor throws.

   Complexity: Linear to the elements x contains.

5.     slist(unspecified x);

   Effects: Move constructor. Moves mx's resources to *this.

   Throws: If allocator_type's default constructor throws.

   Complexity: Constant.

6.     slist& operator=(const slist & x);

   Effects: Makes *this contain the same elements as x.

   Postcondition: this->size() == x.size(). *this contains a copy of each of x's elements.

   Throws: If memory allocation throws or T's copy constructor throws.

   Complexity: Linear to the number of elements in x.

7.     slist& operator=(unspecified mx);

   Effects: Makes *this contain the same elements as x.

   Postcondition: this->size() == x.size(). *this contains a copy of each of x's elements.

   Throws: If memory allocation throws or T's copy constructor throws.

   Complexity: Linear to the number of elements in x.

8.     ~slist();

   Effects: Destroys the list. All stored values are destroyed and used memory is deallocated.

   Throws: Nothing.

   Complexity: Linear to the number of elements.

### `slist` public member functions

1.     allocator_type get_allocator() const;

   Effects: Returns a copy of the internal allocator.

   Throws: If allocator's copy constructor throws.

   Complexity: Constant.

2.     const stored_allocator_type & get_stored_allocator() const;

3.     stored_allocator_type & get_stored_allocator() ;

4.     void assign(size_type n, const T & val) ;

   Effects: Assigns the n copies of val to *this.

   Throws: If memory allocation throws or T's copy constructor throws.

   Complexity: Linear to n.

5.     template<typename InpIt> void assign(InpIt first, InpIt last) ;

   Effects: Assigns the range [first, last) to *this.

   Throws: If memory allocation throws or T's constructor from dereferencing InpIt throws.

   Complexity: Linear to n.

6.     iterator begin() ;

   Effects: Returns an iterator to the first element contained in the list.

   Throws: Nothing.

   Complexity: Constant.

7.     const_iterator begin() const;

   Effects: Returns a const_iterator to the first element contained in the list.

   Throws: Nothing.

   Complexity: Constant.

8.     iterator end() ;

   Effects: Returns an iterator to the end of the list.

   Throws: Nothing.

   Complexity: Constant.

9.     const_iterator end() const;

   Effects: Returns a const_iterator to the end of the list.

   Throws: Nothing.

   Complexity: Constant.

10.     iterator before_begin() ;

    Effects: Returns a non-dereferenceable iterator that, when incremented, yields begin(). This iterator may be used as the argument toinsert_after, erase_after, etc.

    Throws: Nothing.

    Complexity: Constant.

11.     const_iterator before_begin() const;

    Effects: Returns a non-dereferenceable const_iterator that, when incremented, yields begin(). This iterator may be used as the argument toinsert_after, erase_after, etc.

    Throws: Nothing.

    Complexity: Constant.

12.     size_type size() const;

    Effects: Returns the number of the elements contained in the list.

    Throws: Nothing.

    Complexity: Constant.

13.     size_type max_size() const;

    Effects: Returns the largest possible size of the list.

    Throws: Nothing.

    Complexity: Constant.

14.     bool empty() const;

    Effects: Returns true if the list contains no elements.

    Throws: Nothing.

    Complexity: Constant.

15.     void swap(slist & x) ;

    Effects: Swaps the contents of *this and x. If this->allocator_type() != x.allocator_type() allocators are also swapped.

    Throws: Nothing.

    Complexity: Linear to the number of elements on *this and x.

16.     reference front() ;

    Requires: !empty()

    Effects: Returns a reference to the first element from the beginning of the container.

    Throws: Nothing.

    Complexity: Constant.

17.     const_reference front() const;

    Requires: !empty()

    Effects: Returns a const reference to the first element from the beginning of the container.

    Throws: Nothing.

    Complexity: Constant.

18.     void push_front(const value_type & x) ;

    Effects: Inserts a copy of t in the beginning of the list.

    Throws: If memory allocation throws or T's copy constructor throws.

    Complexity: Amortized constant time.

19.     void push_front(unspecified x) ;

    Effects: Constructs a new element in the beginning of the list and moves the resources of t to this new element.

    Throws: If memory allocation throws.

    Complexity: Amortized constant time.

20.     void pop_front() ;

    Effects: Removes the first element from the list.

    Throws: Nothing.

    Complexity: Amortized constant time.

21.     iterator previous(iterator p) ;

    Returns: The iterator to the element before i in the sequence. Returns the end-iterator, if either i is the begin-iterator or the sequence is empty.

    Throws: Nothing.

    Complexity: Linear to the number of elements before i.

22.     const_iterator previous(const_iterator p) ;

    Returns: The const_iterator to the element before i in the sequence. Returns the end-const_iterator, if either i is the begin-const_iterator or the sequence is empty.

    Throws: Nothing.

    Complexity: Linear to the number of elements before i.

23.     iterator insert_after(iterator prev_pos, const value_type & x) ;

    Requires: p must be a valid iterator of *this.

    Effects: Inserts a copy of the value after the p pointed by prev_p.

    Returns: An iterator to the inserted element.

    Throws: If memory allocation throws or T's copy constructor throws.

    Complexity: Amortized constant time.

    Note: Does not affect the validity of iterators and references of previous values.

24.     iterator insert_after(iterator prev_pos, unspecified x) ;

    Requires: prev_pos must be a valid iterator of *this.

    Effects: Inserts a move constructed copy object from the value after the p pointed by prev_pos.

    Returns: An iterator to the inserted element.

    Throws: If memory allocation throws.

    Complexity: Amortized constant time.

    Note: Does not affect the validity of iterators and references of previous values.

25.     void insert_after(iterator prev_pos, size_type n, const value_type & x) ;

    Requires: prev_pos must be a valid iterator of *this.

    Effects: Inserts n copies of x after prev_pos.

    Throws: If memory allocation throws or T's copy constructor throws.

    Complexity: Linear to n.

    Note: Does not affect the validity of iterators and references of previous values.

26.     template<typename InIter>
          void insert_after(iterator prev_pos, InIter first, InIter last) ;

    Requires: prev_pos must be a valid iterator of *this.

    Effects: Inserts the range pointed by [first, last) after the p prev_pos.

    Throws: If memory allocation throws, T's constructor from a dereferenced InpIt throws.

    Complexity: Linear to the number of elements inserted.

    Note: Does not affect the validity of iterators and references of previous values.

27.     iterator insert(iterator p, const value_type & x) ;

    Requires: p must be a valid iterator of *this.

    Effects: Insert a copy of x before p.

    Throws: If memory allocation throws or x's copy constructor throws.

    Complexity: Linear to the elements before p.

28.     iterator insert(iterator p, unspecified x) ;

    Requires: p must be a valid iterator of *this.

    Effects: Insert a new element before p with mx's resources.

    Throws: If memory allocation throws.

    Complexity: Linear to the elements before p.

29.     void insert(iterator p, size_type n, const value_type & x) ;

    Requires: p must be a valid iterator of *this.

    Effects: Inserts n copies of x before p.

    Throws: If memory allocation throws or T's copy constructor throws.

    Complexity: Linear to n plus linear to the elements before p.

30.     template<typename InIter> void insert(iterator p, InIter first, InIter last) ;

    Requires: p must be a valid iterator of *this.

    Effects: Insert a copy of the [first, last) range before p.

    Throws: If memory allocation throws, T's constructor from a dereferenced InpIt throws.

    Complexity: Linear to std::distance [first, last) plus linear to the elements before p.

31.     iterator erase_after(iterator prev_pos) ;

    Effects: Erases the element after the element pointed by prev_pos of the list.

    Returns: the first element remaining beyond the removed elements, or end() if no such element exists.

    Throws: Nothing.

    Complexity: Constant.

    Note: Does not invalidate iterators or references to non erased elements.

32.     iterator erase_after(iterator before_first, iterator last) ;

    Effects: Erases the range (before_first, last) from the list.

    Returns: the first element remaining beyond the removed elements, or end() if no such element exists.

    Throws: Nothing.

    Complexity: Linear to the number of erased elements.

    Note: Does not invalidate iterators or references to non erased elements.

33.     iterator erase(iterator p) ;

    Requires: p must be a valid iterator of *this.

    Effects: Erases the element at p p.

    Throws: Nothing.

    Complexity: Linear to the number of elements before p.

34.     iterator erase(iterator first, iterator last) ;

    Requires: first and last must be valid iterator to elements in *this.

    Effects: Erases the elements pointed by [first, last).

    Throws: Nothing.

    Complexity: Linear to the distance between first and last plus linear to the elements before first.

35.     void resize(size_type new_size, const T & x) ;

    Effects: Inserts or erases elements at the end such that the size becomes n. New elements are copy constructed from x.

    Throws: If memory allocation throws, or T's copy constructor throws.

    Complexity: Linear to the difference between size() and new_size.

36.     void resize(size_type new_size) ;

    Effects: Inserts or erases elements at the end such that the size becomes n. New elements are default constructed.

    Throws: If memory allocation throws, or T's copy constructor throws.

    Complexity: Linear to the difference between size() and new_size.

37.     void clear() ;

    Effects: Erases all the elements of the list.

    Throws: Nothing.

    Complexity: Linear to the number of elements in the list.

38.     void splice_after(iterator prev_pos, slist & x) ;

    Requires: p must point to an element contained by the list. x != *this

    Effects: Transfers all the elements of list x to this list, after the the element pointed by p. No destructors or copy constructors are called.

    Throws: std::runtime_error if this' allocator and x's allocator are not equal.

    Complexity: Linear to the elements in x.

    Note: Iterators of values obtained from list x now point to elements of this list. Iterators of this list and all the references are not invalidated.

39.     void splice_after(iterator prev_pos, slist & x, iterator prev) ;

    Requires: prev_pos must be a valid iterator of this. i must point to an element contained in list x.

    Effects: Transfers the value pointed by i, from list x to this list, after the element pointed by prev_pos. If prev_pos == prev or prev_pos == ++prev, this function is a null operation.

    Throws: std::runtime_error if this' allocator and x's allocator are not equal.

    Complexity: Constant.

    Note: Iterators of values obtained from list x now point to elements of this list. Iterators of this list and all the references are not invalidated.

40.     void splice_after(iterator prev_pos, slist & x, iterator before_first,
                          iterator before_last) ;

    Requires: prev_pos must be a valid iterator of this. before_first and before_last must be valid iterators of x. prev_pos must not be contained in [before_first, before_last) range.

    Effects: Transfers the range [before_first + 1, before_last + 1) from list x to this list, after the element pointed by prev_pos.

    Throws: std::runtime_error if this' allocator and x's allocator are not equal.

    Complexity: Linear to the number of transferred elements.

    Note: Iterators of values obtained from list x now point to elements of this list. Iterators of this list and all the references are not invalidated.

41.     void splice_after(iterator prev_pos, slist & x, iterator before_first,
                          iterator before_last, size_type n) ;

    Requires: prev_pos must be a valid iterator of this. before_first and before_last must be valid iterators of x. prev_pos must not be contained in [before_first, before_last) range. n == std::distance(before_first, before_last)

    Effects: Transfers the range [before_first + 1, before_last + 1) from list x to this list, after the element pointed by prev_pos.

    Throws: std::runtime_error if this' allocator and x's allocator are not equal.

    Complexity: Constant.

    Note: Iterators of values obtained from list x now point to elements of this list. Iterators of this list and all the references are not invalidated.

42.     void splice(iterator p, slist & x) ;

    Requires: p must point to an element contained by the list. x != *this

    Effects: Transfers all the elements of list x to this list, before the the element pointed by p. No destructors or copy constructors are called.

    Throws: std::runtime_error if this' allocator and x's allocator are not equal.

    Complexity: Linear in distance(begin(), p), and linear in x.size().

    Note: Iterators of values obtained from list x now point to elements of this list. Iterators of this list and all the references are not invalidated.

43.     void splice(iterator p, slist & x, iterator i) ;

    Requires: p must point to an element contained by this list. i must point to an element contained in list x.

    Effects: Transfers the value pointed by i, from list x to this list, before the the element pointed by p. No destructors or copy constructors are called. If p == i or p == ++i, this function is a null operation.

    Throws: std::runtime_error if this' allocator and x's allocator are not equal.

    Complexity: Linear in distance(begin(), p), and in distance(x.begin(), i).

    Note: Iterators of values obtained from list x now point to elements of this list. Iterators of this list and all the references are not invalidated.

44.     void splice(iterator p, slist & x, iterator first, iterator last) ;

    Requires: p must point to an element contained by this list. first and last must point to elements contained in list x.

    Effects: Transfers the range pointed by first and last from list x to this list, before the the element pointed by p. No destructors or copy constructors are called.

    Throws: std::runtime_error if this' allocator and x's allocator are not equal.

    Complexity: Linear in distance(begin(), p), in distance(x.begin(), first), and in distance(first, last).

    Note: Iterators of values obtained from list x now point to elements of this list. Iterators of this list and all the references are not invalidated.

45.     void reverse() ;

    Effects: Reverses the order of elements in the list.

    Throws: Nothing.

    Complexity: This function is linear time.

    Note: Iterators and references are not invalidated

46.     void remove(const T & value) ;

    Effects: Removes all the elements that compare equal to value.

    Throws: Nothing.

    Complexity: Linear time. It performs exactly size() comparisons for equality.

    Note: The relative order of elements that are not removed is unchanged, and iterators to elements that are not removed remain valid.

47.     template<typename Pred> void remove_if(Pred pred) ;

    Effects: Removes all the elements for which a specified predicate is satisfied.

    Throws: If pred throws.

    Complexity: Linear time. It performs exactly size() calls to the predicate.

    Note: The relative order of elements that are not removed is unchanged, and iterators to elements that are not removed remain valid.

48.     void unique() ;

    Effects: Removes adjacent duplicate elements or adjacent elements that are equal from the list.

    Throws: Nothing.

    Complexity: Linear time (size()-1 comparisons calls to pred()).

    Note: The relative order of elements that are not removed is unchanged, and iterators to elements that are not removed remain valid.

49.     template<typename Pred> void unique(Pred pred) ;

    Effects: Removes adjacent duplicate elements or adjacent elements that satisfy some binary predicate from the list.

    Throws: If pred throws.

    Complexity: Linear time (size()-1 comparisons equality comparisons).

    Note: The relative order of elements that are not removed is unchanged, and iterators to elements that are not removed remain valid.

50.     void merge(slist & x) ;

    Requires: The lists x and *this must be distinct.

    Effects: This function removes all of x's elements and inserts them in order into *this according to std::less<value_type>. The merge is stable; that is, if an element from *this is equivalent to one from x, then the element from *this will precede the one from x.

    Throws: Nothing.

    Complexity: This function is linear time: it performs at most size() + x.size() - 1 comparisons.

51.     template<typename StrictWeakOrdering>
          void merge(slist & x, StrictWeakOrdering comp) ;

    Requires: p must be a comparison function that induces a strict weak ordering and both *this and x must be sorted according to that ordering The lists x and *this must be distinct.

    Effects: This function removes all of x's elements and inserts them in order into *this. The merge is stable; that is, if an element from *this is equivalent to one from x, then the element from *this will precede the one from x.

    Throws: Nothing.

    Complexity: This function is linear time: it performs at most size() + x.size() - 1 comparisons.

    Note: Iterators and references to *this are not invalidated.

52.     void sort() ;

    Effects: This function sorts the list *this according to std::less<value_type>. The sort is stable, that is, the relative order of equivalent elements is preserved.

    Throws: Nothing.

    Notes: Iterators and references are not invalidated.

    Complexity: The number of comparisons is approximately N log N, where N is the list's size.

53.     template<typename StrictWeakOrdering> void sort(StrictWeakOrdering comp) ;

    Effects: This function sorts the list *this according to std::less<value_type>. The sort is stable, that is, the relative order of equivalent elements is preserved.

    Throws: Nothing.

    Notes: Iterators and references are not invalidated.

    Complexity: The number of comparisons is approximately N log N, where N is the list's size.