STL deque的构造函数(2)

原文地址:http://www.cplusplus.com/reference/deque/deque/deque/public member function
<deque>

std::deque::deque

  • C++98
  • C++11
  • C++14
default (1)
explicit deque (const allocator_type& alloc = allocator_type());
fill (2)
explicit deque (size_type n);
         deque (size_type n, const value_type& val,
                const allocator_type& alloc = allocator_type());
range (3)
template <class InputIterator>
  deque (InputIterator first, InputIterator last,
         const allocator_type& alloc = allocator_type());
copy (4)
deque (const deque& x);
deque (const deque& x, const allocator_type& alloc);
move (5)
deque (deque&& x);
deque (deque&& x, const allocator_type& alloc);
initializer list (6)
deque (initializer_list<value_type> il,
       const allocator_type& alloc = allocator_type());
Construct deque container

Constructs a deque container object, initializing its contents depending on the constructor version used:

deque容器的构造器。初始化的内容取决于以下的版本。


  • C++98
  • C++11
(1) empty container constructor (default constructor)
Constructs an empty container, with no elements.
构造一个空的deque,里面没有元素
(2) fill constructor
Constructs a container with n elements. Each element is a copy of val (if provided).
构造一个含有n个元素,并且每个元素的值都是val的拷贝(如果提供了val的值)。
(3) range constructor
Constructs a container with as many elements as the range [first,last), with each element emplace-constructed from its corresponding element in that range, in the same order.
使用一个范围内的元素构造deque,里面的每一个元素都对应于范围内的元素,并且顺序一样。
(4) copy constructor (and copying with allocator)
Constructs a container with a copy of each of the elements in x, in the same order.
从另一个deque中复制其元素。
(5) move constructor (and moving with allocator)
Constructs a container that acquires the elements of x.
从x中构造。
If alloc is specified and is different from x's allocator, the elements are moved. Otherwise, no elements are constructed (their ownership is directly transferred).
如果x的内存分配其不一样,那么将移动元素,否则,将只是交换所有权。
x is left in an unspecified but valid state.
(6) initializer list constructor
Constructs a container with a copy of each of the elements in il, in the same order.
从初始化列表中构造。

The container keeps an internal copy of alloc, which is used to allocate and deallocate storage for its elements, and to construct and destroy them (as specified by its allocator_traits). If no alloc argument is passed to the constructor, a default-constructed allocator is used, except in the following cases:
容器使用其内部内存分配器的拷贝作为分配以及释放元素内存或者构造以及析构的分配器,如果没有alloc参数,将使用默认的分配器,除非是以下情况:

- The copy constructor  (4, first signature)  creates a container that keeps and uses a copy of the allocator returned by calling the appropriate  selected_on_container_copy_construction  trait on  x 's allocator.


- The move constructor (5, first signature) acquires x's allocator.

All elements are copiedmoved or otherwise constructed by calling allocator_traits::construct with the appropriate arguments.


相应的例子:

(1)

#include <iostream>
#include <deque>
using namespace std;
int main()
{
	deque<int>  d;
	cout<<"d.size="<<d.size()<<endl;
	cout<<"d=";
	for(int i:d)
		cout<<i<<" ";
	cout<<endl;
}
运行截图:

STL deque的构造函数(2)_第1张图片

(2)

#include <iostream>
#include <deque>
using namespace std;
int main()
{
	deque<int>  d(10);
	cout<<"d.size="<<d.size()<<endl;
	cout<<"d=";
	for(int i:d)
		cout<<i<<" ";
	cout<<endl;
	deque<int>  d2(6,8);
	cout<<"d2.size="<<d.size()<<endl;
	cout<<"d2=";
	for(int i:d2)
		cout<<i<<" ";
	cout<<endl;
}
运行截图:

STL deque的构造函数(2)_第2张图片

(3)

#include <iostream>
#include <deque>
#include <vector>
using namespace std;
int main()
{
	int ar[5]={1,2,3,4,5};
	deque<int>  d(ar,ar+5);
	cout<<"d.size="<<d.size()<<endl;
	cout<<"d=";
	for(int i:d)
		cout<<i<<" ";
	cout<<endl;
	vector<double> vd{0.1,0.2,.05,.07,0.9};
	deque<double>  d2(vd.begin()+1,vd.end());
	cout<<"d2.size="<<d.size()<<endl;
	cout<<"d2=";
	for(auto i:d2)
		cout<<i<<" ";
	cout<<endl;
}
运行截图:

STL deque的构造函数(2)_第3张图片

(4)

#include <iostream>
#include <deque>
using namespace std;
int main()
{
	deque<int>  d(4,5);
	cout<<"d.size="<<d.size()<<endl;
	cout<<"d=";
	for(int i:d)
		cout<<i<<" ";
	cout<<endl;
	deque<int>  d2(d);
	cout<<"d2.size="<<d.size()<<endl;
	cout<<"d2=";
	for(auto i:d2)
		cout<<i<<" ";
	cout<<endl;
}
运行截图:

STL deque的构造函数(2)_第4张图片
(5)

#include <iostream>
#include <deque>
using namespace std;
deque<int> returnDeque(){
	deque<int>  d(4,5);	
	cout<<"d.size="<<d.size()<<endl;
	cout<<"d=";
	for(int i:d)
		cout<<i<<" ";
	cout<<endl;
	return d;
}
int main()
{
	
	deque<int>  d2(returnDeque());
	cout<<"d2.size="<<d2.size()<<endl;
	cout<<"d2=";
	for(auto i:d2)
		cout<<i<<" ";
	cout<<endl;
	cout<<endl<<"call returnDeque() twice!"<<endl;
	returnDeque();
}
运行截图:

STL deque的构造函数(2)_第5张图片
可以看出,在这里x依旧有效。

(6)

#include <iostream>
#include <deque>
#include <initializer_list>
using namespace std;
int main()
{
	
	deque<int>  d2({1,2,3,4,5,6,7});
	cout<<"d2.size="<<d2.size()<<endl;
	cout<<"d2=";
	for(auto i:d2)
		cout<<i<<" ";
	cout<<endl;
}
运行截图:

STL deque的构造函数(2)_第6张图片

Parameters

alloc

Allocator object.

The container keeps and uses an internal copy of this allocator.
Member type allocator_type is the internal allocator type used by the container, defined in deque as an alias of its second template parameter (Alloc).
If allocator_type is an instantiation of the default allocator (which has no state), this is not relevant.

内存分配器对象。

容器使用该对象的拷贝。

n
Initial container size (i.e., the number of elements in the container at construction).

Member type size_type is an unsigned integral type.

初始化容器的大小。

val
Value to fill the container with. Each of the n elements in the container will be initialized to a copy of this value.

Member type value_type is the type of the elements in the container, defined in deque as an alias of its first template parameter (T).

用于填满容器内的值,容器内每个元素都将被初始化为val的拷贝。

first, last
Input iterators to the initial and final positions in a range. The range used is [first,last), which includes all the elements between first and last, including the element pointed by first but not the element pointed by last.

The function template argument InputIterator shall be an input iterator type that points to elements of a type from whichvalue_type objects can be constructed.

初始化的范围的开头以及结尾的迭代器,包括first的元素,但不包括last。

x

Another deque object of the same type (with the same class template arguments T and Alloc), whose contents are either copied or acquired.

另一个同类型的deque(相同的模版参数)

il
An initializer_list object.
These objects are automatically constructed from initializer list declarators.

Member type value_type is the type of the elements in the container, defined in deque as an alias of its first template parameter (T).

一个初始化列表对象。


Example

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 
// constructing deques #include <iostream> #include <deque> int main () { unsigned int i; // constructors used in the same order as described above: std::deque<int> first; // empty deque of ints std::deque<int> second (4,100); // four ints with value 100 std::deque<int> third (second.begin(),second.end()); // iterating through second std::deque<int> fourth (third); // a copy of third // the iterator constructor can be used to copy arrays: int myints[] = {16,2,77,29}; std::deque<int> fifth (myints, myints + sizeof(myints) / sizeof(int) ); std::cout << "The contents of fifth are:"; for (std::deque<int>::iterator it = fifth.begin(); it!=fifth.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; }
Edit & Run

Output:
The contents of fifth are: 16 2 77 29 

Complexity

Constant for the default constructor (1), and for the move constructors (5) (unless alloc is different from x's allocator).
For all other cases, linear in the resulting container size.

Iterator validity

The move constructors (5), invalidate all iterators, pointers and references related to x if the elements are moved.

Data races

All copied elements are accessed.
The move constructors (5) modify x.

Exception safety

Strong guarantee: no effects in case an exception is thrown.

If allocator_traits::construct is not supported with the appropriate arguments for the element constructions, or if the range specified by[first,last) is not valid, it causes undefined behavior.



——————————————————————————————————————————————————————————————————

//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双

Email:[email protected]

2014-9-1

于GDUT

——————————————————————————————————————————————————————————————————




你可能感兴趣的:(STL,STL,C++11,deque,deque)