| Home | Libraries | People | FAQ | More

## Class template flat_map

boost::container::flat_map

## Synopsis

    // In header: <boost/container/flat_map.hpp>

    template<typename Key, typename T,
             typename Pred = std::less< std::pair< Key, T> >,
             typename A = std::allocator<T> >
    class flat_map {
    public:
      // types
      typedef impl_tree_t::key_type              key_type;
      typedef T                                  mapped_type;
      typedef std::pair< key_type, mapped_type > value_type;
      typedef A::pointer                         pointer;
      typedef A::const_pointer                   const_pointer;
      typedef A::reference                       reference;
      typedef A::const_reference                 const_reference;
      typedef unspecified                        value_compare;
      typedef Pred                               key_compare;
      typedef unspecified                        iterator;
      typedef unspecified                        const_iterator;
      typedef unspecified                        reverse_iterator;
      typedef unspecified                        const_reverse_iterator;
      typedef impl_tree_t::size_type             size_type;
      typedef impl_tree_t::difference_type       difference_type;
      typedef A                                  allocator_type;
      typedef A                                  stored_allocator_type;

      // construct/copy/destruct
      explicit flat_map(const Pred & = Pred(),
                        const allocator_type & = allocator_type());
      template<typename InputIterator>
        flat_map(InputIterator, InputIterator, const Pred & = Pred(),
                 const allocator_type & = allocator_type());
      template<typename InputIterator>
        flat_map(ordered_unique_range_t, InputIterator, InputIterator,
                 const Pred & = Pred(),
                 const allocator_type & = allocator_type());
      flat_map(const flat_map< Key, T, Pred, A > &);
      flat_map(BOOST_RV_REF(flat_map));
      flat_map& operator=(BOOST_COPY_ASSIGN_REF(flat_map));
      flat_map& operator=(BOOST_RV_REF(flat_map));

      // public member functions
      key_compare key_comp() const;
      value_compare value_comp() const;
      allocator_type get_allocator() const;
      const stored_allocator_type & get_stored_allocator() const;
      stored_allocator_type & get_stored_allocator();
      iterator begin();
      const_iterator begin() const;
      const_iterator cbegin() const;
      iterator end();
      const_iterator end() const;
      const_iterator cend() const;
      reverse_iterator rbegin();
      const_reverse_iterator rbegin() const;
      const_reverse_iterator crbegin() const;
      reverse_iterator rend();
      const_reverse_iterator rend() const;
      const_reverse_iterator crend() const;
      bool empty() const;
      size_type size() const;
      size_type max_size() const;
      T & operator[](const key_type &);
      T & operator[](BOOST_RV_REF(key_type));
      T & at(const key_type &);
      const T & at(const key_type &) const;
      void swap(flat_map &);
      std::pair< iterator, bool > insert(const value_type &);
      std::pair< iterator, bool > insert(BOOST_RV_REF(value_type));
      std::pair< iterator, bool > insert(BOOST_RV_REF(impl_value_type));
      iterator insert(const_iterator, const value_type &);
      iterator insert(const_iterator, BOOST_RV_REF(value_type));
      iterator insert(const_iterator, BOOST_RV_REF(impl_value_type));
      template<typename InputIterator> void insert(InputIterator, InputIterator);
      template<class... Args> iterator emplace(Args &&...);
      template<class... Args> iterator emplace_hint(const_iterator, Args &&...);
      iterator emplace();
      iterator emplace_hint(const_iterator);
      iterator erase(const_iterator);
      size_type erase(const key_type &);
      iterator erase(const_iterator, const_iterator);
      void clear();
      void shrink_to_fit();
      iterator find(const key_type &);
      const_iterator find(const key_type &) const;
      size_type count(const key_type &) const;
      iterator lower_bound(const key_type &);
      const_iterator lower_bound(const key_type &) const;
      iterator upper_bound(const key_type &);
      const_iterator upper_bound(const key_type &) const;
      std::pair< iterator, iterator > equal_range(const key_type &);
      std::pair< const_iterator, const_iterator >
      equal_range(const key_type &) const;
      size_type capacity() const;
      void reserve(size_type);
    };

## Description

A flat_map is a kind of associative container that supports unique keys (contains at most one of each key value) and provides for fast retrieval of values of another type T based on the keys. The flat_map class supports random-access iterators.

A flat_map satisfies all of the requirements of a container and of a reversible container and of an associative container. A flat_map also provides most operations described for unique keys. For a flat_map<Key,T> the key_type is Key and the value_type is std::pair<Key,T> (unlike std::map<Key, T> which value_type is std::pair<const Key, T>).

Pred is the ordering function for Keys (e.g. std::less<Key>).

A is the allocator to allocate the value_types (e.g. allocator< std::pair<Key, T> >).

flat_map is similar to std::map but it's implemented like an ordered vector. This means that inserting a new element into a flat_map invalidates previous iterators and references

Erasing an element of a flat_map invalidates iterators and references pointing to elements that come after (their keys are bigger) the erased element.

### `flat_map` public construct/copy/destruct

1.     explicit flat_map(const Pred & comp = Pred(),
                         const allocator_type & a = allocator_type());

   Effects: Constructs an empty `flat_map` using the specified comparison object and allocator.

   Complexity: Constant.

2.     template<typename InputIterator>
         flat_map(InputIterator first, InputIterator last,
                  const Pred & comp = Pred(),
                  const allocator_type & a = allocator_type());

   Effects: Constructs an empty `flat_map` using the specified comparison object and allocator, and inserts elements from the range [first ,last ).

   Complexity: Linear in N if the range [first ,last ) is already sorted using comp and otherwise N logN, where N is last - first.

3.     template<typename InputIterator>
         flat_map(ordered_unique_range_t, InputIterator first, InputIterator last,
                  const Pred & comp = Pred(),
                  const allocator_type & a = allocator_type());

   Effects: Constructs an empty `flat_map` using the specified comparison object and allocator, and inserts elements from the ordered unique range [first ,last). This function is more efficient than the normal range creation for ordered ranges.

   Requires: [first ,last) must be ordered according to the predicate and must be unique values.

   Complexity: Linear in N.

4.     flat_map(const flat_map< Key, T, Pred, A > & x);

   Effects: Copy constructs a `flat_map`.

   Complexity: Linear in x.size().

5.     flat_map(BOOST_RV_REF(flat_map) x);

   Effects: Move constructs a `flat_map`. Constructs *this using x's resources.

   Complexity: Construct.

   Postcondition: x is emptied.

6.     flat_map& operator=(BOOST_COPY_ASSIGN_REF(flat_map) x);

   Effects: Makes *this a copy of x.

   Complexity: Linear in x.size().

7.     flat_map& operator=(BOOST_RV_REF(flat_map) mx);

   Effects: Move constructs a `flat_map`. Constructs *this using x's resources.

   Complexity: Construct.

   Postcondition: x is emptied.

### `flat_map` public member functions

1.     key_compare key_comp() const;

   Effects: Returns the comparison object out of which a was constructed.

   Complexity: Constant.

2.     value_compare value_comp() const;

   Effects: Returns an object of value_compare constructed out of the comparison object.

   Complexity: Constant.

3.     allocator_type get_allocator() const;

   Effects: Returns a copy of the Allocator that was passed to the object's constructor.

   Complexity: Constant.

4.     const stored_allocator_type & get_stored_allocator() const;

5.     stored_allocator_type & get_stored_allocator();

6.     iterator begin();

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

   Throws: Nothing.

   Complexity: Constant.

7.     const_iterator begin() const;

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

   Throws: Nothing.

   Complexity: Constant.

8.     const_iterator cbegin() const;

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

   Throws: Nothing.

   Complexity: Constant.

9.     iterator end();

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

   Throws: Nothing.

   Complexity: Constant.

10.     const_iterator end() const;

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

    Throws: Nothing.

    Complexity: Constant.

11.     const_iterator cend() const;

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

    Throws: Nothing.

    Complexity: Constant.

12.     reverse_iterator rbegin();

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

    Throws: Nothing.

    Complexity: Constant.

13.     const_reverse_iterator rbegin() const;

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

    Throws: Nothing.

    Complexity: Constant.

14.     const_reverse_iterator crbegin() const;

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

    Throws: Nothing.

    Complexity: Constant.

15.     reverse_iterator rend();

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

    Throws: Nothing.

    Complexity: Constant.

16.     const_reverse_iterator rend() const;

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

    Throws: Nothing.

    Complexity: Constant.

17.     const_reverse_iterator crend() const;

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

    Throws: Nothing.

    Complexity: Constant.

18.     bool empty() const;

    Effects: Returns true if the container contains no elements.

    Throws: Nothing.

    Complexity: Constant.

19.     size_type size() const;

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

    Throws: Nothing.

    Complexity: Constant.

20.     size_type max_size() const;

    Effects: Returns the largest possible size of the container.

    Throws: Nothing.

    Complexity: Constant.

21.     T & operator[](const key_type & k);

    Effects: If there is no key equivalent to x in the `flat_map`, inserts value_type(x, T()) into the `flat_map`.

    Returns: A reference to the mapped_type corresponding to x in *this.

    Complexity: Logarithmic.

22.     T & operator[](BOOST_RV_REF(key_type) mk);

    Effects: If there is no key equivalent to x in the `flat_map`, inserts value_type(move(x), T()) into the `flat_map` (the key is move-constructed)

    Returns: A reference to the mapped_type corresponding to x in *this.

    Complexity: Logarithmic.

23.     T & at(const key_type & k);

    Returns: A reference to the element whose key is equivalent to x. Throws: An exception object of type out_of_range if no such element is present. Complexity: logarithmic.

24.     const T & at(const key_type & k) const;

    Returns: A reference to the element whose key is equivalent to x. Throws: An exception object of type out_of_range if no such element is present. Complexity: logarithmic.

25.     void swap(flat_map & x);

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

    Throws: Nothing.

    Complexity: Constant.

26.     std::pair< iterator, bool > insert(const value_type & x);

    Effects: Inserts x if and only if there is no element in the container with key equivalent to the key of x.

    Returns: The bool component of the returned pair is true if and only if the insertion takes place, and the iterator component of the pair points to the element with key equivalent to the key of x.

    Complexity: Logarithmic search time plus linear insertion to the elements with bigger keys than x.

    Note: If an element it's inserted it might invalidate elements.

27.     std::pair< iterator, bool > insert(BOOST_RV_REF(value_type) x);

    Effects: Inserts a new value_type move constructed from the pair if and only if there is no element in the container with key equivalent to the key of x.

    Returns: The bool component of the returned pair is true if and only if the insertion takes place, and the iterator component of the pair points to the element with key equivalent to the key of x.

    Complexity: Logarithmic search time plus linear insertion to the elements with bigger keys than x.

    Note: If an element it's inserted it might invalidate elements.

28.     std::pair< iterator, bool > insert(BOOST_RV_REF(impl_value_type) x);

    Effects: Inserts a new value_type move constructed from the pair if and only if there is no element in the container with key equivalent to the key of x.

    Returns: The bool component of the returned pair is true if and only if the insertion takes place, and the iterator component of the pair points to the element with key equivalent to the key of x.

    Complexity: Logarithmic search time plus linear insertion to the elements with bigger keys than x.

    Note: If an element it's inserted it might invalidate elements.

29.     iterator insert(const_iterator position, const value_type & x);

    Effects: Inserts a copy of x in the container if and only if there is no element in the container with key equivalent to the key of x. p is a hint pointing to where the insert should start to search.

    Returns: An iterator pointing to the element with key equivalent to the key of x.

    Complexity: Logarithmic search time (constant if x is inserted right before p) plus insertion linear to the elements with bigger keys than x.

    Note: If an element it's inserted it might invalidate elements.

30.     iterator insert(const_iterator position, BOOST_RV_REF(value_type) x);

    Effects: Inserts an element move constructed from x in the container. p is a hint pointing to where the insert should start to search.

    Returns: An iterator pointing to the element with key equivalent to the key of x.

    Complexity: Logarithmic search time (constant if x is inserted right before p) plus insertion linear to the elements with bigger keys than x.

    Note: If an element it's inserted it might invalidate elements.

31.     iterator insert(const_iterator position, BOOST_RV_REF(impl_value_type) x);

    Effects: Inserts an element move constructed from x in the container. p is a hint pointing to where the insert should start to search.

    Returns: An iterator pointing to the element with key equivalent to the key of x.

    Complexity: Logarithmic search time (constant if x is inserted right before p) plus insertion linear to the elements with bigger keys than x.

    Note: If an element it's inserted it might invalidate elements.

32.     template<typename InputIterator>
          void insert(InputIterator first, InputIterator last);

    Requires: first, last are not iterators into *this.

    Effects: inserts each element from the range [first,last) if and only if there is no element with key equivalent to the key of that element.

    Complexity: At most N log(size()+N) (N is the distance from first to last) search time plus N*size() insertion time.

    Note: If an element it's inserted it might invalidate elements.

33.     template<class... Args> iterator emplace(Args &&... args);

    Effects: Inserts an object of type T constructed with std::forward<Args>(args)... if and only if there is no element in the container with key equivalent to the key of x.

    Returns: The bool component of the returned pair is true if and only if the insertion takes place, and the iterator component of the pair points to the element with key equivalent to the key of x.

    Complexity: Logarithmic search time plus linear insertion to the elements with bigger keys than x.

    Note: If an element it's inserted it might invalidate elements.

34.     template<class... Args>
          iterator emplace_hint(const_iterator hint, Args &&... args);

    Effects: Inserts an object of type T constructed with std::forward<Args>(args)... in the container if and only if there is no element in the container with key equivalent to the key of x. p is a hint pointing to where the insert should start to search.

    Returns: An iterator pointing to the element with key equivalent to the key of x.

    Complexity: Logarithmic search time (constant if x is inserted right before p) plus insertion linear to the elements with bigger keys than x.

    Note: If an element it's inserted it might invalidate elements.

35.     iterator emplace();

36.     iterator emplace_hint(const_iterator hint);

37.     iterator erase(const_iterator position);

    Effects: Erases the element pointed to by position.

    Returns: Returns an iterator pointing to the element immediately following q prior to the element being erased. If no such element exists, returns end().

    Complexity: Linear to the elements with keys bigger than position

    Note: Invalidates elements with keys not less than the erased element.

38.     size_type erase(const key_type & x);

    Effects: Erases all elements in the container with key equivalent to x.

    Returns: Returns the number of erased elements.

    Complexity: Logarithmic search time plus erasure time linear to the elements with bigger keys.

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

    Effects: Erases all the elements in the range [first, last).

    Returns: Returns last.

    Complexity: size()*N where N is the distance from first to last.

    Complexity: Logarithmic search time plus erasure time linear to the elements with bigger keys.

40.     void clear();

    Effects: erase(a.begin(),a.end()).

    Postcondition: size() == 0.

    Complexity: linear in size().

41.     void shrink_to_fit();

    Effects: Tries to deallocate the excess of memory created

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

    Complexity: Linear to size().

42.     iterator find(const key_type & x);

    Returns: An iterator pointing to an element with the key equivalent to x, or end() if such an element is not found.

    Complexity: Logarithmic.

43.     const_iterator find(const key_type & x) const;

    Returns: A const_iterator pointing to an element with the key equivalent to x, or end() if such an element is not found.

    Complexity: Logarithmic.s

44.     size_type count(const key_type & x) const;

    Returns: The number of elements with key equivalent to x.

    Complexity: log(size())+count(k)

45.     iterator lower_bound(const key_type & x);

    Returns: An iterator pointing to the first element with key not less than k, or a.end() if such an element is not found.

    Complexity: Logarithmic

46.     const_iterator lower_bound(const key_type & x) const;

    Returns: A const iterator pointing to the first element with key not less than k, or a.end() if such an element is not found.

    Complexity: Logarithmic

47.     iterator upper_bound(const key_type & x);

    Returns: An iterator pointing to the first element with key not less than x, or end() if such an element is not found.

    Complexity: Logarithmic

48.     const_iterator upper_bound(const key_type & x) const;

    Returns: A const iterator pointing to the first element with key not less than x, or end() if such an element is not found.

    Complexity: Logarithmic

49.     std::pair< iterator, iterator > equal_range(const key_type & x);

    Effects: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)).

    Complexity: Logarithmic

50.     std::pair< const_iterator, const_iterator >
        equal_range(const key_type & x) const;

    Effects: Equivalent to std::make_pair(this->lower_bound(k), this->upper_bound(k)).

    Complexity: Logarithmic

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

52.     void reserve(size_type count);

    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.

    Note: If capacity() is less than "count", iterators and references to to values might be invalidated.