Online Book Reader

Home Category

Professional C__ - Marc Gregoire [308]

By Root 1302 0

}

protected:

Compare comp;

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

};

// The iterator class needs access to protected members of hashmap

friend class HashIterator;

// Constructors

explicit hashmap(const Compare& comp = Compare(),

const Hash& hash = Hash()) throw(invalid_argument);

template

hashmap(InputIterator first, InputIterator last,

const Compare& comp = Compare(), const Hash& hash = Hash())

throw(invalid_argument);

// destructor, copy constructor, move constructor,

// copy assignment operator and move assignment operator

~hashmap();

hashmap(const hashmap& src);

hashmap(hashmap&& src); // C++11

hashmap& operator=(

const hashmap& rhs);

hashmap& operator=(

hashmap&& rhs); // C++11

// C++11 initializer list constructor

hashmap(initializer_list il,

const Compare& comp = Compare(),

const Hash& hash = Hash()) throw(invalid_argument);

// C++11 initializer list assignment operator

hashmap& operator=(

initializer_list il);

// Iterator methods

iterator begin();

iterator end();

const_iterator begin() const;

const_iterator end() const;

const_iterator cbegin() const; // For C++11

const_iterator cend() const; // For C++11

// Size methods

bool empty() const;

size_type size() const;

size_type max_size() const;

// Element insert methods

T& operator[] (const key_type& x);

pair insert(const value_type& x);

iterator insert(iterator position, const value_type& x);

template

void insert(InputIterator first, InputIterator last);

void insert(initializer_list il); // C++11

// C++11 emplace methods

pair emplace(value_type&& x);

iterator emplace_hint(iterator hint, value_type&& x);

// Element delete methods

void erase(iterator position);

size_type erase(const key_type& x);

void erase(iterator first, iterator last);

// Other modifying utilities

void swap(hashmap& hashIn);

void clear();

// Access methods for STL conformity

key_compare key_comp() const;

value_compare value_comp() const;

// Lookup methods

iterator find(const key_type& x);

const_iterator find(const key_type& x) const;

size_type count(const key_type& x) const;

protected:

typedef list ListType;

typename ListType::iterator findElement(

const key_type& x, size_t& bucket) const;

vector* mElems;

size_type mSize;

Compare mComp;

Hash mHash;

};

Code snippet from Hashmap\FinalHashmap\hashmap.h

hashmap Constructors

The implementation of the default constructor is shown earlier. The second constructor is a method template so that it can take an iterator range from any container, not just other hashmaps. If it were not a method template, it would need to specify the InputIterator type explicitly as HashIterator, limiting it to iterators from hashmaps. Despite the syntax, the implementation is uncomplicated: It initializes all the data members, then calls insert() to actually insert all the elements in the specified range:

// Make a call to insert() to actually insert the elements.

template

template

hashmap::hashmap(

InputIterator first, InputIterator last, const Compare& comp,

const Hash& hash) throw(invalid_argument) : mSize(0), mComp(comp), mHash(hash)

{

if (mHash.numBuckets() <= 0) {

throw invalid_argument("Number of buckets must be positive");

}

mElems = new vector(mHash.numBuckets());

insert(first, last);

}

Code snippet from Hashmap\FinalHashmap\hashmap.cpp

hashmap Initializer List Constructor

C++11 supports initializer lists, which are discussed in Chapter 9. Following is the implementation of the hashmap constructor that takes an initializer list, which is very similar to the implementation of the constructor accepting an iterator range:

template

Return Main Page Previous Page Next Page

®Online Book Reader