Zum Inhalt springen

C++-Referenz/ Standardbibliothek/ Container-Klassen/ map

Aus Wikibooks
Alte Seite

Die Arbeit am Buch »C++-Referenz« wurde vom Hauptautor eingestellt. Ein Lehrbuch zum Thema C++ ist unter »C++-Programmierung« zu finden. Eine sehr umfangreiche und gute Referenz gibt es unter cppreference.com.

Diese Seite beschreibt C++98, einen stark veralteten Standard. Aktuelle Referenz.

// Header: map
template <typename Key, typename T, typename Compare = less<Key>, typename Allocator = allocator<T> >
class map{
public:
    typedef Key                                   key_type;
    typedef T                                     mapped_type;
    typedef pair<Key const, T>                    value_type;
    typedef Compare                               key_compare;
    typedef Allocator                             allocator_type;
    typedef typename Allocator::reference         reference;
    typedef typename Allocator::const_reference   const_reference;
    typedef implementation defined                iterator;
    typedef implementation defined                const_iterator;
    typedef typename Allocator::size_type         size_type;
    typedef typename Allocator::difference_type   difference_type;
    typedef Allocator::pointer                    pointer;
    typedef Allocator::const_pointer              const_pointer;
    typedef std::reverse_iterator<iterator>       reverse_iterator;
    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

    class value_compare: public binary_function<value_type,value_type,bool>{
    public:
        bool operator()(value_type const& x, value_type const& y)const{
            return comp(x.first, y.first);
        }

    protected:
        Compare comp;
        value_compare(Compare c):comp(c){}

    friend class map;
    };

// Konstruktoren, Destruktor, Kopieren
    explicit map(Compare const& comp = Compare(), Allocator const& = Allocator());

    template <typename InputIterator> map(
        InputIterator first,
        InputIterator last,
        Compare const& comp = Compare(),
        Allocator const& = Allocator()
    );

    map(map<Key,T,Compare,Allocator> const& x);

    ~map();

    map<Key,T,Compare,Allocator>& operator=(map<Key,T,Compare,Allocator> const& x);

// Iteratoren
    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;

// Kapazität
    size_type size()const;
    size_type max_size()const;
    bool      empty()const;

// Elementzugriff
    reference operator[](key_type const& x);

// Modifizierer
    pair<iterator, bool> insert(value_type const& x);
    iterator             insert(iterator position, value_type const& x);
    template <typename InputIterator>
    void                 insert(InputIterator first, InputIterator last);

    void      erase(iterator position);
    size_type erase(key_type const& x);
    void      erase(iterator first, iterator last);

    void swap(map<Key,T,Compare,Allocator>&);
    void clear();

// Betrachter
    key_compare   key_comp()const;
    value_compare value_comp()const;

// map-Operationen
    iterator       find(key_type const& x);
    const_iterator find(key_type const& x)const;

    size_type count(key_type const& x)const;

    iterator       lower_bound(key_type const& x);
    const_iterator lower_bound(key_type const& x)const;
    iterator       upper_bound(key_type const& x);
    const_iterator upper_bound(key_type const& x)const;

    pair<iterator,iterator>             equal_range(key_type const& x);
    pair<const_iterator,const_iterator> equal_range(key_type const& x)const;
};

// Vergleiche
template <typename Key, typename T, typename Compare, typename Allocator>
bool operator==(map<Key,T,Compare,Allocator> const& x, map<Key,T,Compare,Allocator> const& y);

template <typename Key, typename T, typename Compare, typename Allocator>
bool operator< (map<Key,T,Compare,Allocator> const& x, map<Key,T,Compare,Allocator> const& y);

template <typename Key, typename T, typename Compare, typename Allocator>
bool operator!=(map<Key,T,Compare,Allocator> const& x, map<Key,T,Compare,Allocator> const& y);

template <typename Key, typename T, typename Compare, typename Allocator>
bool operator> (map<Key,T,Compare,Allocator> const& x, map<Key,T,Compare,Allocator> const& y);

template <typename Key, typename T, typename Compare, typename Allocator>
bool operator>=(map<Key,T,Compare,Allocator> const& x, map<Key,T,Compare,Allocator> const& y);

template <typename Key, typename T, typename Compare, typename Allocator>
bool operator<=(map<Key,T,Compare,Allocator> const& x, map<Key,T,Compare,Allocator> const& y);

// Spezialisierte Algorithmen
template <typename Key, typename T, typename Compare, typename Allocator>
void swap(map<Key,T,Compare,Allocator>& x, map<Key,T,Compare,Allocator>& y);