patterncppModerate
Implementation of std::map
Viewed 0 times
stdimplementationmap
Problem
I have decided to make a basic C++ implementation of the
```
template , typename _Alloc = std::allocator > > class map
{
public:
typedef map _Myt;
typedef _Key key_type;
typedef _Ty mapped_type;
typedef _Cmp compare_type;
typedef _Alloc allocator_type;
typedef std::pair value_type;
typedef value_type *pointer;
typedef const value_type *const_pointer;
typedef value_type *iterator;
typedef const value_type *const_iterator;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
map()
: size_(0), capacity_(20), data_(_Alloc().allocate(20))
{
}
map(const _Myt &_Rhs)
: size_(_Rhs.size_), capacity_(_Rhs.size_ + 20), data_(_Alloc().allocate(_Rhs.size_))
{
int count = 0;
for (iterator i = &_Rhs.data_[0]; i != &_Rhs.data_[_Rhs.size_]; ++i, ++count)
{
_Alloc().construct(&data_[count], *i);
}
}
~map()
{
if (!empty())
{
for (iterator i = begin(); i != end(); ++i)
{
_Alloc().destroy(i);
}
_Alloc().deallocate(data_, capacity_);
}
}
_Myt &insert(const value_type &_Value)
{
if (++size_ >= capacity_)
{
reserve(capacity_ * 2);
}
_Alloc().construct(&data_[size_ - 1], _Value);
return *this;
}
bool has_key(const key_type &_Key)
{
for (iterator i = begin(); i != end(); ++i)
{
if (i->first == _Key)
{
return true;
}
}
return false;
}
mapped_type &operator[](const key_type &_Key)
{
if (has_key(_Key))
{
for (iterator i = begin(); i != end(); ++i)
{
if (i->first == _Ke
std::map class, and I was checking that it is fine, as I am not sure if the operator[] is done correctly: should I be using the has_key function? e.t.c```
template , typename _Alloc = std::allocator > > class map
{
public:
typedef map _Myt;
typedef _Key key_type;
typedef _Ty mapped_type;
typedef _Cmp compare_type;
typedef _Alloc allocator_type;
typedef std::pair value_type;
typedef value_type *pointer;
typedef const value_type *const_pointer;
typedef value_type *iterator;
typedef const value_type *const_iterator;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
map()
: size_(0), capacity_(20), data_(_Alloc().allocate(20))
{
}
map(const _Myt &_Rhs)
: size_(_Rhs.size_), capacity_(_Rhs.size_ + 20), data_(_Alloc().allocate(_Rhs.size_))
{
int count = 0;
for (iterator i = &_Rhs.data_[0]; i != &_Rhs.data_[_Rhs.size_]; ++i, ++count)
{
_Alloc().construct(&data_[count], *i);
}
}
~map()
{
if (!empty())
{
for (iterator i = begin(); i != end(); ++i)
{
_Alloc().destroy(i);
}
_Alloc().deallocate(data_, capacity_);
}
}
_Myt &insert(const value_type &_Value)
{
if (++size_ >= capacity_)
{
reserve(capacity_ * 2);
}
_Alloc().construct(&data_[size_ - 1], _Value);
return *this;
}
bool has_key(const key_type &_Key)
{
for (iterator i = begin(); i != end(); ++i)
{
if (i->first == _Key)
{
return true;
}
}
return false;
}
mapped_type &operator[](const key_type &_Key)
{
if (has_key(_Key))
{
for (iterator i = begin(); i != end(); ++i)
{
if (i->first == _Ke
Solution
Readability
With your lengthy
Naming standards
According to this answer, identifiers in the form
7.1.3 Reserved identifiers
Each header declares or defines all identifiers listed in its
associated subclause, and optionally declares or defines identifiers
listed in its associated future library directions subclause and
identifiers which are always reserved either for any use or for use as
file scope identifiers.
letter or another underscore are always reserved for any use.
[...]
Const-correctness
-
You have iterators, but you should also have
-
-
With your lengthy
template statement, I'd put the class statement onto the next line:template
class mapNaming standards
According to this answer, identifiers in the form
_Identifier are reserved:7.1.3 Reserved identifiers
Each header declares or defines all identifiers listed in its
associated subclause, and optionally declares or defines identifiers
listed in its associated future library directions subclause and
identifiers which are always reserved either for any use or for use as
file scope identifiers.
- All identifiers that begin with an underscore and either an uppercase
letter or another underscore are always reserved for any use.
[...]
Const-correctness
-
You have iterators, but you should also have
const iterators:const_iterator cbegin() const
{
return &data_[0];
}
const_iterator cend() const
{
return &data_[size_];
}-
empty() should be const:bool empty() const
{
return size_ == 0;
}-
has_key() should be const and use the aforementioned const iterators:bool has_key(const key_type &_Key) const
{
for (const_iterator i = cbegin(); i != cend(); ++i)
{
if (i->first == _Key)
{
return true;
}
}
return false;
}Code Snippets
template </*...*/>
class mapconst_iterator cbegin() const
{
return &data_[0];
}
const_iterator cend() const
{
return &data_[size_];
}bool empty() const
{
return size_ == 0;
}bool has_key(const key_type &_Key) const
{
for (const_iterator i = cbegin(); i != cend(); ++i)
{
if (i->first == _Key)
{
return true;
}
}
return false;
}Context
StackExchange Code Review Q#43972, answer score: 10
Revisions (0)
No revisions yet.