Online Book Reader

Home Category

Professional C__ - Marc Gregoire [307]

By Root 1153 0
object of type value_compare. The constructors should provide a default constructed object as the default value. n log n

Constructor taking an initializer_list as parameter. Constructs the container and inserts the elements from the initializer list into the container. n log n

Assignment operator with an initializer_list as right-hand side. Replaces all elements from the container with the elements from the initializer list n log n

key_compare key_comp()

const;

value_compare

value_comp() const; Returns the comparison objects for comparing just keys or entire values constant

pair

insert(value_type&);

iterator insert(

iterator,

value_type&);

void insert(

InputIterator start,

InputIterator end); Three different forms of insert.

The iterator position in the second is a hint, which can be ignored.

The range in the third need not be from a container of the same type.

Containers that allow duplicate keys return just iterator from the first form, because insert() always succeeds. logarithmic except for the second form, which can be amortized constant if the element is inserted immediately before the given hint.

void insert(

initializer_list

); Inserts the elements from the initializer list into the container. logarithmic

pair

emplace(

value_type&&);

iterator emplace_hint(

iterator hint,

value_type&&); Implements the C++11 emplace operations to construct objects in-place. In-place construction is discussed in Chapter 12. logarithmic, except for the second form, which can be amortized constant if the element is inserted immediately before the given hint.

size_type

erase(key_type&);

void erase(iterator);

void erase(

iterator start,

iterator end); Three different forms of erase.

The first form returns the number of values erased (0 or 1, in containers that do not allow duplicate keys).

The second and third forms erase the elements at iterator position, or in the range start to end. logarithmic, except for the second form, which should be amortized constant

void clear(); Erases all elements linear

iterator

find(key_type&);

const_iterator

find(key_type&)

const; Finds the element with the specified key logarithmic

size_type

count(key_type&)

const; Returns the number of elements with the specified key (0 or 1 in containers that do not allow duplicate keys) logarithmic

iterator lower_bound(

key_type&);

iterator upper_bound(

key_type&);

pair

equal_range(

key_type&);

const_iterator

lower_bound(

key_type&) const;

const_iterator

upper_bound(

key_type&) const;

pairconst_iterator>

equal_range(

key_type&) const; Returns iterators referring to the first element of the specified key, one past the last element of the specified key, or both logarithmic

Note that the collection methods lower_bound(), upper_bound(), and equal_range() only make sense on sorted containers. Thus the hashmap class does not need to provide them.

Here is the complete hashmap class definition. Note that the prototypes for insert(), erase(), and find() need to change slightly from the previous versions shown, because those initial versions don’t have the right return types required for associative containers:

template ,

typename Hash = DefaultHash>

class hashmap

{

public:

typedef Key key_type;

typedef T mapped_type;

typedef pair value_type;

typedef Compare key_compare;

typedef pair& reference;

typedef const pair& const_reference;

typedef HashIterator iterator;

typedef HashIterator const_iterator;

typedef size_t size_type;

typedef ptrdiff_t difference_type;

// Required class definition for associative containers

class value_compare :

public std::binary_function

{

friend class hashmap;

public:

bool operator() (const value_type& x, const value_type& y) const

{

return comp(x.first, y.first);

Return Main Page Previous Page Next Page

®Online Book Reader