Reputation: 1549
using namespace std::rel_ops;
template <typename T, typename A = std::allocator<T> >
class my_vector {
public:
typedef A allocator_type;
typedef typename allocator_type::value_type value_type;
typedef typename allocator_type::size_type size_type;
typedef typename allocator_type::difference_type difference_type;
typedef typename allocator_type::pointer pointer;
typedef typename allocator_type::const_pointer const_pointer;
typedef typename allocator_type::reference reference;
typedef typename allocator_type::const_reference const_reference;
typedef typename allocator_type::pointer iterator;
typedef typename allocator_type::const_pointer const_iterator;
public:
friend bool operator == (const my_vector& lhs, const my_vector& rhs) {
return (lhs.size() == rhs.size()) && std::equal(lhs.begin(), lhs.end(), rhs.begin());}
friend bool operator < (const my_vector& lhs, const my_vector& rhs) {
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());}
friend void swap (my_vector& x, my_vector& y) {
x.swap(y);}
private:
allocator_type _a;
pointer _b;
pointer _e; // size
pointer _l; // capacity
private:
bool valid () const {
return (!_b && !_e && !_l) || ((_b <= _e) && (_e <= _l));}
my_vector (const my_vector& that, size_type c) :
_a (that._a) {
assert(c >= that.size());
_b = _a.allocate(c);
_e = _b + that.size();
_l = _b + c;
my_uninitialized_copy(_a, that.begin(), that.end(), begin());
assert(valid());}
I was going over this snippet of code, and there are many things that I don't understand, since I am new to C++.
bool valid () const
", what is this line, "(!_b && !_e && !_l)
" trying to do?It's negating the pointers, and I don't know what it does, and what's it's trying to accomplish.
In line, "my_vector (const my_vector& that, size_type c) :
," what is the type of 'that
'? Is it a reference to its own class?
In "my_vector (const my_vector& that, size_type c) :
", what is this line doing, "_a (that._a) :
" ? Is '_a
' an Allocator object? because below this line, there is "_a.allocate(c),
" and 'allocate' is a member function of Allocator.
What's the purpose of having both const_pointer and pointer? Also, both reference and const_reference, and both iterator and const_iterator? in the first public under the class my_vector?
Upvotes: 1
Views: 476
Reputation: 24936
1. The statement
!_x
... (where x can be b
, e
or l
.) is true if the pointer _x
is 0
, NULL
or nullptr
.
It negates the value of the result of the cast from pointer to boolean. Therefore, (!_b && !_e && !_l)
is true
if none of the pointers is set.
2)
const my_vector& that
...
... is a const referent to my_vector
(which is actually the current class, so this is probably a constructor that copies the object with additional information handed in via c
.
3) The object
_a
...
... is declared as allocator_type _a;
, where allocator_type
is a typedef for A
which is a template argument defaulted to std::allocator<T>
.
Have a look:
template <typename T, typename A = std::allocator<T>>
...
typedef A allocator_type;
...
allocator_type _a;
...
The expression in question (comment) is a member intialization list (with only 1 element)
my_vector (const my_vector& that, size_type c) : _a (that._a)
{
// ...
}
It basically means "initialize _a
with the value of that._a
" in this case, since both have the same type, the copy constructor of _a
is invoked.
The purpose of such typedefs is to enable gneric programming.
template<class Container>
void insert_sorted (Container & object,
typename Container::const_reference value)
{
typename Container::size_type const N = object.size(),
find_index = find_sorted(object, value);
if (find_index < N)
object.insert(object.begin()+find_index, value);
else object.push_back(value);
}
The code is valid only if the Container for which it is used defines
const_reference
size_type
insert()
that takes an iterator and a const_reference
push_back()
that takes a const_reference
Upvotes: 2