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' は Allocator オブジェクトですか? この行の下に " _a.allocate(c)," があり、'allocate' は Allocator のメンバ関数であるためです。

  3. const_pointer とポインタの両方を持つ目的は何ですか? また、reference と const_reference の両方、および iterator と const_iterator の両方ですか? クラス my_vector の下の最初のパブリックで?

4

1 に答える 1

2

1. 声明!_x

... (x はbeまたはl.) は、ポインター_x0NULLまたはの場合に true ですnullptr

ポインターからブール値へのキャストの結果の値を否定します。したがって、どのポインタも設定されていない場合です(!_b && !_e && !_l)true

2) const my_vector& that...

... への const 参照先my_vector(これは実際には現在のクラスであるため、これはおそらくc.

3) オブジェクト_a...

... として宣言されていますallocator_type _a;。ここで、はテンプレート引数allocator_typeの typedef で、デフォルトはです。Astd::allocator<T>

見てください:

template <typename T, typename A = std::allocator<T>>
...
typedef A allocator_type;
...
allocator_type _a;
...

問題の式 (コメント) は、メンバーの初期化リスト (1 つの要素のみ) です。

 my_vector (const my_vector& that, size_type c) : _a (that._a) 
 {
 // ...
 }

この場合は基本的に「_aの値で初期化する」という意味that._aで、どちらも同じ型なので、 のコピーコンストラクタ_aが呼び出されます。


このような typedef の目的は、ジェネリック プログラミングを有効にすることです。

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

コードは、それが使用されるコンテナが定義する場合にのみ有効です

  • タイプ(定義)const_reference
  • タイプ(定義)size_type
  • insert()イテレータとconst_reference
  • push_back()を取るメソッドconst_reference
于 2013-07-24T02:04:01.093 に答える