c++ - class template, allocator_traits -
using namespace std::rel_ops; template <typename t, typename = std::allocator<t> > class my_vector { public: typedef 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 going on snippet of code, , there many things don't understand, since new c++.
- in "
bool valid () const", line, "(!_b && !_e && !_l)" trying do?
it's negating pointers, , don't know does, , what's it's trying accomplish.
in line, "
my_vector (const my_vector& that, size_type c) :," type of 'that'? reference own class?in "
my_vector (const my_vector& that, size_type c) :", line doing, "_a (that._a) :" ? '_a' allocator object? because below line, there "_a.allocate(c)," , 'allocate' member function of allocator.what's purpose of having both const_pointer , pointer? also, both reference , const_reference, , both iterator , const_iterator? in first public under class my_vector?
1. statement
!_x
... (where x can b, e or l.) true if pointer _x 0, null or nullptr.
it negates value of result of cast pointer boolean. therefore, (!_b && !_e && !_l) true if none of pointers set.
2)
const my_vector& that...
... const referent my_vector (which current class, constructor copies object additional information handed in via c.
3) object
_a...
... declared allocator_type _a;, allocator_type typedef a template argument defaulted std::allocator<t>.
have look:
template <typename t, typename = std::allocator<t>> ... typedef allocator_type; ... allocator_type _a; ... the expression in question (comment) member intialization list (with 1 element)
my_vector (const my_vector& that, size_type c) : _a (that._a) { // ... } it means "initialize _a value of that._a" in case, since both have same type, copy constructor of _a invoked.
the purpose of such typedefs 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 valid if container used defines
- a type(def)
const_reference - a type(def)
size_type - a method
insert()takes iterator ,const_reference - a method
push_back()takesconst_reference
Comments
Post a Comment