- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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++ 的新手。
bool valid () const
”中,这行“(!_b && !_e && !_l)
”试图做什么?它正在否定指针,我不知道它做了什么,也不知道它试图完成什么。
在行中,“my_vector (const my_vector& that, size_type c) :
”,“that
”的类型是什么?它是对其自身类的引用吗?
在“my_vector (const my_vector& that, size_type c) :
”中,这一行是做什么的,“_a (that._a) :
”? “_a
”是分配器对象吗?因为在这一行下面,有“_a.allocate(c),
”,'allocate'是Allocator的一个成员函数。
同时拥有 const_pointer 和 pointer 的目的是什么?还有,reference 和 const_reference,以及 iterator 和 const_iterator?在 my_vector 类下的第一个公共(public)?
最佳答案
1. The statement
!_x
...(其中 x 可以是 b
、 e
或 l
。)如果指针 _x
为真是0
, NULL
或 nullptr
.
它否定从指针到 bool 值的转换结果值。因此,(!_b && !_e && !_l)
是true
如果没有设置任何指针。
2)
const my_vector& that
...
... 是对 my_vector
的常量引用(这实际上是当前类,所以这可能是一个构造函数,它复制带有通过 c
传递的附加信息的对象。
3) The object
_a
...
... 声明为 allocator_type _a;
, 其中allocator_type
是 A
的类型定义这是默认为 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);
}
该代码仅在使用它的容器定义时才有效
const_reference
size_type
insert()
它需要一个迭代器和一个 const_reference
push_back()
这需要 const_reference
关于c++ - 类模板,allocator_traits,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17823983/
C++11 提供了 std::allocator_traits 类作为使用分配器的标准方式。静态函数 std::allocator_traits::construct() 将一个指针 指向应该构造对象
using namespace std::rel_ops; template > class my_vector { public: typedef A
如果Allocator符合标准库分配器接口(interface),调用std::allocator_traits::deallocate是否安全用空指针?我知道是什么时候 Allocator是std:
std::allocator_traits当我提供一个带有单个模板参数的分配器的 STL 样式容器时,它会自动发挥它的魔力,但当我提供一个带有两个模板参数但其他方面相似的分配器的 STL 样式容器时,
我想设计一个类模板,它采用分配器类型(如标准第 17.6.3.5 节中所定义)作为模板参数。我怎么看std::allocator_traits有助于填补 A 的任何缺失成员使用默认设置。除此之外,标准
我想定制std::vector不默认构造元素类型的行为(例如 int ),因为对大型 vector 执行此操作的成本很高。 看看这个,我能看到的唯一方法就是专门化 std::allocator_tra
受到 Haskell 的启发,我尝试像这样实现 std::forward_list: namespace std { template > class forward_list {
分配器可以选择嵌套类型,如 pointer , const_pointer .但是可以始终将这些接口(interface)与 std::allocator_traits 一起使用,如果这些类型在 Al
我正在尝试实现一个 STL 风格的容器类,我有一个关于在我的类中使用分配器的问题: STL 的 allocator_traits 中的静态成员函数有何用途? 直到现在,我认为我应该实例化 alloca
我不了解 C++ 11 中的 allocator_traits,所以我尝试了 allocator_traits exmple来自 cplusplus 网站。但是,此代码无法在 gcc 4.9、VS 2
我有一个结构定义为: struct Foo { double x; double y; }; 我有一个将这个结构作为参数的函数: void doSomethingWithFoo(Foo
我很想理解boost::container::allocator_traits当我遇到 boost::container::allocator_traits::is_partially_propaga
下面的代码可以正常编译: #include #include int main() { const int * a = new int(5); std::cout >; auto alloc =
我想这个问题真的是关于 std::allocator_traits 的设计的并提供自定义分配器。如果我想构造一个 std::shared_ptr<>使用自定义分配器,我可以使用 std::alloca
为什么 C++0x 无序关联容器不使用 allocator_traits 来定义它们的成员类型 pointer 和 const_pointer? 例如,顺序关联容器和有序关联容器使用以下定义: typ
我正在尝试重新绑定(bind)我的自定义分配器类型,MyAllocator , 用于 basic_string类,例如: std::basic_string, MyAllocator> ... 分配器
为什么没有标准 C++03 接口(interface)用于查询 C++0x 中使用的分配器的成员类型?成员类型不足的用例有哪些? 最佳答案 为了从设计模式的角度解释 allocator_traits,
根据 http://en.cppreference.com/w/cpp/concept/Allocator ,一个可以用作分配器类型的类必须满足许多要求。但是,我找不到 C++ 标准指定的相同要求。该
我是一名优秀的程序员,十分优秀!