1
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());}

我正在查看这段代码,因为我是 C++ 新手,所以有很多东西我不明白。

  1. 在“ bool valid () const”中,这条线“ (!_b && !_e && !_l)”试图做什么?

它否定了指针,我不知道它做了什么,它试图完成什么。

  1. 在行中,“ my_vector (const my_vector& that, size_type c) :”是什么类型的 ' that'?它是对它自己的类的引用吗?

  2. 在“ my_vector (const my_vector& that, size_type c) :”中,这条线在做什么,“ _a (that._a) :”?' _a' 是一个分配器对象吗?因为在这一行下面,有“ _a.allocate(c),”,而“分配”是分配器的成员函数。

  3. 同时拥有 const_pointer 和指针的目的是什么?此外,reference 和 const_reference,以及 iterator 和 const_iterator?在 my_vector 类下的第一个 public 中?

4

1 回答 1

2

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

  • a type(def) const_reference
  • a type(def) size_type
  • a method insert() that takes an iterator and a const_reference
  • a method push_back() that takes a const_reference
于 2013-07-24T02:04:01.093 回答