| Home | Libraries | People | FAQ | More

## Class template vector

boost::interprocess::vector

## Synopsis

    template<typename T, typename A>
    class vector {
    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 unspecified                             iterator;                // The random access iterator.
      typedef unspecified                             const_iterator;          // The random access const_iterator.
      typedef std::reverse_iterator< iterator >       reverse_iterator;        // Iterator used to iterate backwards through a vector.
      typedef std::reverse_iterator< const_iterator > const_reverse_iterator;  // Const iterator used to iterate backwards through a vector.
      typedef allocator_type                          stored_allocator_type;   // The stored allocator type.

      // construct/copy/destruct
      vector(const A & = A());
      vector(size_type, const T & = T(),
             const allocator_type & = allocator_type());
      vector(const vector< T, A > &);
      vector(unspecified);
      template<typename InIt>
        vector(InIt, InIt, const allocator_type & = allocator_type());
      vector& operator=(const vector< T, A > &);
      vector& operator=(unspecified);
      ~vector();

      // public member functions
      iterator begin() ;
      const_iterator begin() const;
      iterator end() ;
      const_iterator end() const;
      reverse_iterator rbegin() ;
      const_reverse_iterator rbegin() const;
      reverse_iterator rend() ;
      const_reverse_iterator rend() const;
      reference front() ;
      const_reference front() const;
      reference back() ;
      const_reference back() const;
      size_type size() const;
      size_type max_size() const;
      size_type capacity() const;
      bool empty() const;
      reference operator[](size_type) ;
      const_reference operator[](size_type) const;
      reference at(size_type) ;
      const_reference at(size_type) const;
      allocator_type get_allocator() const;
      const stored_allocator_type & get_stored_allocator() const;
      stored_allocator_type & get_stored_allocator() ;
      void reserve(size_type) ;
      void assign(size_type, const value_type &) ;
      template<typename InIt> void assign(InIt, InIt) ;
      void push_back(const T &) ;
      void push_back(unspecified) ;
      void swap(vector< T, A > &) ;
      void swap(unspecified) ;
      iterator insert(iterator, const T &) ;
      iterator insert(iterator, unspecified) ;
      template<typename InIt> void insert(iterator, InIt, InIt) ;
      void insert(iterator, size_type, const T &) ;
      void pop_back() ;
      iterator erase(const_iterator) ;
      iterator erase(const_iterator, const_iterator) ;
      void resize(size_type, const T &) ;
      void resize(size_type) ;
      void clear() ;
    };

## Description

A vector is a sequence that supports random access to elements, constant time insertion and removal of elements at the end, and linear time insertion and removal of elements at the beginning or in the middle. The number of elements in a vector may vary dynamically; memory management is automatic. boost::interprocess::vector is similar to std::vector but it's compatible with shared memory and memory mapped files.

### `vector` public types

1. typedef T value_type;

   The type of object, T, stored in the vector

### `vector` public construct/copy/destruct

1.     vector(const A & a = A());

   Effects: Constructs a vector taking the allocator as parameter.

   Throws: If allocator_type's copy constructor throws.

   Complexity: Constant.

2.     vector(size_type n, const T & value = T(),
              const allocator_type & a = allocator_type());

   Effects: Constructs a vector 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.     vector(const vector< T, A > & x);

   Effects: Copy constructs a vector.

   Postcondition: x == *this.

   Complexity: Linear to the elements x contains.

4.     vector(unspecified mx);

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

   Throws: If allocator_type's copy constructor throws.

   Complexity: Constant.

5.     template<typename InIt>
         vector(InIt first, InIt last, const allocator_type & a = allocator_type());

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

   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).

6.     vector& operator=(const vector< T, A > & 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.     vector& operator=(unspecified mx);

   Effects: Move assignment. All mx's values are transferred to *this.

   Postcondition: x.empty(). *this contains a the elements x had before the function.

   Throws: If allocator_type's copy constructor throws.

   Complexity: Constant.

8.     ~vector();

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

   Throws: Nothing.

   Complexity: Linear to the number of elements.

### `vector` public member functions

1.     iterator begin() ;

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

   Throws: Nothing.

   Complexity: Constant.

2.     const_iterator begin() const;

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

   Throws: Nothing.

   Complexity: Constant.

3.     iterator end() ;

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

   Throws: Nothing.

   Complexity: Constant.

4.     const_iterator end() const;

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

   Throws: Nothing.

   Complexity: Constant.

5.     reverse_iterator rbegin() ;

   Effects: Returns a reverse_iterator pointing to the beginning of the reversed vector.

   Throws: Nothing.

   Complexity: Constant.

6.     const_reverse_iterator rbegin() const;

   Effects: Returns a const_reverse_iterator pointing to the beginning of the reversed vector.

   Throws: Nothing.

   Complexity: Constant.

7.     reverse_iterator rend() ;

   Effects: Returns a reverse_iterator pointing to the end of the reversed vector.

   Throws: Nothing.

   Complexity: Constant.

8.     const_reverse_iterator rend() const;

   Effects: Returns a const_reverse_iterator pointing to the end of the reversed vector.

   Throws: Nothing.

   Complexity: Constant.

9.     reference front() ;

   Requires: !empty()

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

   Throws: Nothing.

   Complexity: Constant.

10.     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.

11.     reference back() ;

    Requires: !empty()

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

    Throws: Nothing.

    Complexity: Constant.

12.     const_reference back() const;

    Requires: !empty()

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

    Throws: Nothing.

    Complexity: Constant.

13.     size_type size() const;

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

    Throws: Nothing.

    Complexity: Constant.

14.     size_type max_size() const;

    Effects: Returns the largest possible size of the vector.

    Throws: Nothing.

    Complexity: Constant.

15.     size_type capacity() const;

    Effects: Number of elements for which memory has been allocated. capacity() is always greater than or equal to size().

    Throws: Nothing.

    Complexity: Constant.

16.     bool empty() const;

    Effects: Returns true if the vector contains no elements.

    Throws: Nothing.

    Complexity: Constant.

17.     reference operator[](size_type n) ;

    Requires: size() < n.

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

    Throws: Nothing.

    Complexity: Constant.

18.     const_reference operator[](size_type n) const;

    Requires: size() < n.

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

    Throws: Nothing.

    Complexity: Constant.

19.     reference at(size_type n) ;

    Requires: size() < n.

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

    Throws: std::range_error if n >= size()

    Complexity: Constant.

20.     const_reference at(size_type n) const;

    Requires: size() < n.

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

    Throws: std::range_error if n >= size()

    Complexity: Constant.

21.     allocator_type get_allocator() const;

    Effects: Returns a copy of the internal allocator.

    Throws: If allocator's copy constructor throws.

    Complexity: Constant.

22.     const stored_allocator_type & get_stored_allocator() const;

23.     stored_allocator_type & get_stored_allocator() ;

24.     void reserve(size_type new_cap) ;

    Effects: If n is less than or equal to capacity(), this call has no effect. Otherwise, it is a request for allocation of additional memory. If the request is successful, then capacity() is greater than or equal to n; otherwise, capacity() is unchanged. In either case, size() is unchanged.

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

25.     void assign(size_type n, const value_type & 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.

26.     template<typename InIt> void assign(InIt first, InIt last) ;

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

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

    Complexity: Linear to n.

27.     void push_back(const T & x) ;

    Effects: Inserts a copy of x at the end of the vector.

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

    Complexity: Amortized constant time.

28.     void push_back(unspecified mx) ;

    Effects: Constructs a new element in the end of the vector and moves the resources of mx to this new element.

    Throws: If memory allocation throws.

    Complexity: Amortized constant time.

29.     void swap(vector< T, A > & x) ;

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

    Throws: Nothing.

    Complexity: Constant.

30.     void swap(unspecified mx) ;

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

    Throws: Nothing.

    Complexity: Constant.

31.     iterator insert(iterator position, const T & x) ;

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

    Effects: Insert a copy of x before position.

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

    Complexity: If position is begin() or end(), amortized constant time Linear time otherwise.

32.     iterator insert(iterator position, unspecified mx) ;

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

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

    Throws: If memory allocation throws.

    Complexity: If position is begin() or end(), amortized constant time Linear time otherwise.

33.     template<typename InIt> void insert(iterator pos, InIt first, InIt last) ;

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

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

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

    Complexity: Linear to std::distance [first, last).

34.     void insert(iterator p, size_type n, const T & x) ;

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

    Effects: Insert n copies of x before pos.

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

    Complexity: Linear to n.

35.     void pop_back() ;

    Effects: Removes the last element from the vector.

    Throws: Nothing.

    Complexity: Constant time.

36.     iterator erase(const_iterator position) ;

    Effects: Erases the element at position pos.

    Throws: Nothing.

    Complexity: Linear to the elements between pos and the last element. Constant if pos is the first or the last element.

37.     iterator erase(const_iterator first, const_iterator last) ;

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

    Throws: Nothing.

    Complexity: Linear to the distance between first and last.

38.     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.

39.     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.

40.     void clear() ;

    Effects: Erases all the elements of the vector.

    Throws: Nothing.

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