[英]For C++ vector initialization, what's the difference between "vector<int>v = n;" and "vector<int>v(n);"
(English is not my native language; please excuse typing and grammar errors.) (英语不是我的母语;请原谅打字和语法错误。)
I'm trying to create a vector<int>
object with known length n
.我正在尝试创建一个已知长度
n
的vector<int>
object 。
I knew that I could do this by vector<int> v(n);
我知道我可以通过
vector<int> v(n);
来做到这一点。 or vector<int> v = vector<int>(n);
或
vector<int> v = vector<int>(n);
. . However, when I tried to do it by
vector<int> v = n;
但是,当我尝试通过
vector<int> v = n;
, I got an Compile Error. ,我得到一个编译错误。
In my previous experience, vector<int> v = n
seems the same as vector<int> v = vector<int>(n)
, but it proves that I'm wrong.在我之前的经验中,
vector<int> v = n
似乎与vector<int> v = vector<int>(n)
相同,但它证明我错了。
I've read the cpp reference and searched "C++ vector initialize with an integer" on stackoverflow but cannot find much useful information.我已经阅读了 cpp 参考并在 stackoverflow 上搜索了“C++ vector initialize with an integer”,但找不到太多有用的信息。
So what's the difference between the three ways?那么这三种方式有什么区别呢? Thanks in advance.
提前致谢。
vector<int> v(n)
generate a vector named "v" vector<int> v(n)
生成一个名为“v”的向量
vector<int> v
declare a vector named "v" vector<int> v
声明一个名为“v”的向量
vector<int> v = vector<int>(n)
means that you generate a temp vector<int>(n)
and v = temp
. vector<int> v = vector<int>(n)
表示您生成一个临时vector<int>(n)
和v = temp
。 "v" and "temp" have the same type vector<int>
so you can use "=" on them. "v" 和 "temp" 具有相同的类型
vector<int>
所以你可以在它们上使用 "="。
But vector<int> v = n
is vector<int> = int
they don't have the same type.但是
vector<int> v = n
是vector<int> = int
他们没有相同的类型。
Here we consider the statement:在这里,我们考虑以下声明:
vector<int> v = n; //this is copy initialization
The above is copy-initialization .以上是复制初始化。 But the constructor for
std::vector
that take size
as argument is explicit and hence cannot be used here, and so this fails with the error that you're getting.但是将
size
作为参数的std::vector
的构造函数是显式的,因此不能在此处使用,因此这会失败并出现您遇到的错误。
Here we consider the statement:在这里,我们考虑以下声明:
vector<int> v = vector<int>(n); //this is also copy initialization
The above is also copy initialization .以上也是拷贝初始化。 But this time, there is a copy constructor of
std::vector
that takes a vector
as an argument and so this works without any error.但是这一次,有一个
std::vector
的复制构造函数,它接受一个vector
作为参数,所以它可以正常工作而没有任何错误。 Here the vector named v
is created as a copy of( prior C++17 ) the temporary vector on the right hand side.这里名为
v
的向量被创建为右侧临时向量的副本(之前的 C++17 )。
Also note that from C++17 onwards, due to mandatory copy elison , it is guaranteed that v
is constructed directly using the ctor that takes an size
as argument instead of being created as a copy using the copy ctor.另请注意,从 C++17 开始,由于强制复制 elison ,保证
v
是直接使用以size
作为参数的 ctor 构造的,而不是使用复制 ctor 作为副本创建的。
Here we consider the statement:在这里,我们考虑以下声明:
vector<int> v(n); //this is direct initilaization
The above is direct initialization and it creates a vector named v
of size n
.以上是直接初始化,它创建了一个名为
v
的大小为n
的向量。 This works because even though the ctor that takes size
as argument is explicit, it can be used in direct initialization.这是有效的,因为即使将
size
作为参数的 ctor 是显式的,它也可以用于直接初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.