gpt4 book ai didi

c++ - 类模板,allocator_traits

转载 作者:行者123 更新时间:2023-11-30 04:16:17 25 4
gpt4 key购买 nike

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),”,'allocate'是Allocator的一个成员函数。

  3. 同时拥有 const_pointer 和 pointer 的目的是什么?还有,reference 和 const_reference,以及 iterator 和 const_iterator?在 my_vector 类下的第一个公共(public)?

最佳答案

1. The statement !_x

...(其中 x 可以是 bel 。)如果指针 _x 为真是0 , NULLnullptr .

它否定从指针到 bool 值的转换结果值。因此,(!_b && !_e && !_l)true如果没有设置任何指针。

2) const my_vector& that ...

... 是对 my_vector 的常量引用(这实际上是当前类,所以这可能是一个构造函数,它复制带有通过 c 传递的附加信息的对象。

3) The object _a ...

... 声明为 allocator_type _a; , 其中allocator_typeA 的类型定义这是默认为 std::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);
}

该代码仅在使用它的容器定义时才有效

  • 类型(def) const_reference
  • 类型(def) size_type
  • 一个方法insert()它需要一个迭代器和一个 const_reference
  • 一个方法push_back()这需要 const_reference

关于c++ - 类模板,allocator_traits,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17823983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com