简体   繁体   English

对于C++向量初始化,“向量”有什么区别<int> v = n;”和“向量<int> v(n);"</int></int>

[英]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 .我正在尝试创建一个已知长度nvector<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 = nvector<int> = int他们没有相同的类型。

Case 1情况1

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的构造函数是显式的,因此不能在此处使用,因此这会失败并出现您遇到的错误。

Case 2案例2

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 作为副本创建的。

Case 3案例3

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.

相关问题 &quot;vector&quot; 和有什么不一样<int> v[]”和“向量<vector<int> &gt; v&quot; - What is the difference between "vector<int> v[]" and "vector<vector<int>> v" `vector <int> v;`和`vector <int> v = vector <int>();`之间的区别 - Difference between `vector<int> v;` and `vector<int> v = vector<int>();` 向量有什么区别<int> vec[n] 和向量<vector<int> &gt; C++ 中的 vec? - What is the difference in the vector<int> vec[n] and vector<vector<int>> vec in C++? 向量之间的差异<int> V[] 和向量&lt;向量<int> &gt; 五</int></int> - Difference between vector <int> V[] and vector< vector<int> > V 向量和向量有什么区别<int>一、向量<int> a[n] 和向量<int>一个)?</int></int></int> - What is the difference between vector<int> a , vector<int> a[n] and vector<int> a(n)? 黑白差异 std::vector<int> V(N) 和 std::vector<int> V[N]?</int></int> - Difference b/w std::vector<int> V(N) and std::vector<int> V[N]? 向量和向量有什么区别<vector<int> &gt; v 和向量<int> * v 在 memory 配置? </int></vector<int> - What is the difference between vector<vector<int> > v and vector <int>* v in memory allocation? 将向量定义为向量 v(n) 与向量 v[n] 有什么区别? - what is the difference between defining a vector that as vector v(n) vs vector v[n]? 以下两种情况初始化向量v(n)有什么区别 - What is the difference in initializing vector v(n) between the following two cases &quot;矢量&quot; 和有什么不一样<int> &amp; 一个 &quot; 和 &quot; 向量<int>一个”? - what's the difference between "vector<int>& a " and "vector<int> a"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM