创建大小元素的向量,而不调用复制构造函数?
Create vector of size elements, without calling copy constructor?
有没有办法不用调用复制构造函数而是调用元素的默认构造函数来创建 N 个元素的向量?我不想要元素的复制构造函数,因为复制应该被阻止。
这里看起来可以,选项 3:
http://en.cppreference.com/w/cpp/container/vector/vector
3) Constructs the container with count default-inserted instances of
T. No copies are made.
但这里看起来你不能:
http://www.cplusplus.com/reference/vector/vector/vector/
empty container constructor (default constructor) Constructs an empty container, with no elements.
fill constructor Constructs a container with n elements. Each element is a copy of val (if provided).
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.
copy constructor (and copying with allocator) Constructs a container with a copy of each of the elements in x, in the same order.
move constructor (and moving with allocator) Constructs a container that acquires the elements of 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
is left in an unspecified but valid state.
initializer list constructor Constructs a container with a copy of each of the elements in il, in the same order.
是的,这就是矢量的工作原理。
您误读了 cplusplus.com 的措辞。它说 "fill constructor Constructs a container with n elements. Each element is a copy of val
(if provided)";你无法摆脱它。但是你不提供val
!
我们可以通过以下简单代码简单地证明当元素是默认构造时不会(或者至少不需要)创建副本:
#include <vector>
#include <iostream>
struct T
{
T() { std::cout << "def-cons\n"; }
~T() { std::cout << "dest\n"; }
T(const T&) = delete;
};
int main()
{
std::vector<T> v(5);
}
(live demo)
标准本身很明确,元素只需要是默认可构造的,不需要可复制性:
[C++11: 23.3.6.2]:
explicit vector(size_type n);
- Effects: Constructs a
vector
with n
value-initialized elements.
- Requires:
T
shall be DefaultConstructible
.
- Complexity: Linear in
n
.
这取决于您使用的 C++ 版本。在 C++11 之前,唯一的
将元素放入向量的方法是复制:
std::vector<T> v( 10 );
相当于:
std::vector<T> v( 10, T() );
用默认构造T
复制了10次。 C++11改变了这一点,
并且第一种形式需要默认构造T
10次,
没有任何复制。
有没有办法不用调用复制构造函数而是调用元素的默认构造函数来创建 N 个元素的向量?我不想要元素的复制构造函数,因为复制应该被阻止。
这里看起来可以,选项 3:
http://en.cppreference.com/w/cpp/container/vector/vector
3) Constructs the container with count default-inserted instances of T. No copies are made.
但这里看起来你不能:
http://www.cplusplus.com/reference/vector/vector/vector/
empty container constructor (default constructor) Constructs an empty container, with no elements.
fill constructor Constructs a container with n elements. Each element is a copy of val (if provided).
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.
copy constructor (and copying with allocator) Constructs a container with a copy of each of the elements in x, in the same order.
move constructor (and moving with allocator) Constructs a container that acquires the elements of 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 is left in an unspecified but valid state.
initializer list constructor Constructs a container with a copy of each of the elements in il, in the same order.
是的,这就是矢量的工作原理。
您误读了 cplusplus.com 的措辞。它说 "fill constructor Constructs a container with n elements. Each element is a copy of val
(if provided)";你无法摆脱它。但是你不提供val
!
我们可以通过以下简单代码简单地证明当元素是默认构造时不会(或者至少不需要)创建副本:
#include <vector>
#include <iostream>
struct T
{
T() { std::cout << "def-cons\n"; }
~T() { std::cout << "dest\n"; }
T(const T&) = delete;
};
int main()
{
std::vector<T> v(5);
}
(live demo)
标准本身很明确,元素只需要是默认可构造的,不需要可复制性:
[C++11: 23.3.6.2]:
explicit vector(size_type n);
- Effects: Constructs a
vector
withn
value-initialized elements.- Requires:
T
shall beDefaultConstructible
.- Complexity: Linear in
n
.
这取决于您使用的 C++ 版本。在 C++11 之前,唯一的 将元素放入向量的方法是复制:
std::vector<T> v( 10 );
相当于:
std::vector<T> v( 10, T() );
用默认构造T
复制了10次。 C++11改变了这一点,
并且第一种形式需要默认构造T
10次,
没有任何复制。