Constructors
语法:
1 2 3 4 5 |
deque(); deque( size_type size ); deque( size_type num, const TYPE &val ); deque( const deque &from ); deque( input_iterator start, input_iterator end ); |
C++ Deques能用以下方式创建:
无参,创建一个空双向队列
size – 创建一个大小为size的双向队列
num and val – 放置num个val的拷贝到队列中,
from – 从from创建一个内容一样的双向队列
start 和 end – 创建一个队列,保存从start到end的元素。
下列代码创建并显示一个双向队列:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
//源代码 /** * @brief Default constructor creates no elements. */ explicit deque(const allocator_type& __a = allocator_type()) : _Base(__a, 0) {} /** * @brief Create a %deque with copies of an exemplar element. * @param n The number of elements to initially create. * @param value An element to copy. * * This constructor fills the %deque with @a n copies of @a value. */ deque(size_type __n, const value_type& __value, const allocator_type& __a = allocator_type()) : _Base(__a, __n) { _M_fill_initialize(__value); } /** * @brief Create a %deque with default elements. * @param n The number of elements to initially create. * * This constructor fills the %deque with @a n copies of a * default-constructed element. */ explicit deque(size_type __n) : _Base(allocator_type(), __n) { _M_fill_initialize(value_type()); } /** * @brief %Deque copy constructor. * @param x A %deque of identical element and allocator types. * * The newly-created %deque uses a copy of the allocation object used * by @a x. */ deque(const deque& __x) : _Base(__x.get_allocator(), __x.size()) { std::uninitialized_copy(__x.begin(), __x.end(), this->_M_impl._M_start); } /** * @brief Builds a %deque from a range. * @param first An input iterator. * @param last An input iterator. * * Create a %deque consisting of copies of the elements from [first, * last). * * If the iterators are forward, bidirectional, or random-access, then * this will call the elements' copy constructor N times (where N is * distance(first,last)) and do no memory reallocation. But if only * input iterators are used, then this will do at most 2N calls to the * copy constructor, and logN memory reallocations. */ template<typename _InputIterator> deque(_InputIterator __first, _InputIterator __last, const allocator_type& __a = allocator_type()) : _Base(__a) { // Check whether it's an integral type. If so, it's not an iterator. typedef typename _Is_integer<_InputIterator>::_Integral _Integral; _M_initialize_dispatch(__first, __last, _Integral()); } |
测试代码:
测试结果:
dq2双向对列:
0 0 0 0 0 0 0 0 0 0
dq3双向对列:
1 1 1 1 1
dq4双向对列:
1 1 1 1 1
dq5双向对列:
1 1 1 1 1
请按任意键继续. . .
《双向队列Constructors构造函数运用》上有1条评论
评论已关闭。