简体   繁体   English

以下两种情况初始化向量v(n)有什么区别

[英]What is the difference in initializing vector v(n) between the following two cases

I have following piece of code,我有以下一段代码,

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    int input,n;
    cin >> n;
    vector<int> v(n);
    for(int i=0;i<n;i++){
       cin>>input;
       v.push_back(input);
    }   
   for(int i=0; i<v.size();i++){
       cout << v[i] << endl;
   }
   cout << v.size() << endl;
   return 0;
}

and

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    int input,n;
    vector<int> v(n);
    cin >> n;
    for(int i=0;i<n;i++){
       cin>>input;
       v.push_back(input);
    }

   for(int i=0; i<v.size();i++){
       cout << v[i] << endl;
   }
   cout << v.size() << endl;
   return 0;
}

for the following input, n=5, 1 2 3 4 5 , first program gives the output对于以下输入, n=5, 1 2 3 4 5 ,第一个程序给出 output

0
0
0
0
0
1
2
3
4
5
10

and the second program gives the result第二个程序给出了结果

1
2
3
4
5
5

I donot understand why vector is initializing 0's in the first doubling the size but not in the second我不明白为什么向量在第一次将大小加倍时初始化 0,但在第二次中却没有

Actually, neither of those are correct for what you want (I'm assuming here you want the five elements you input and nothing more - that may be an invalid assumption but it seems like a good bet to me).实际上,这些都不适合您想要的(我在这里假设您想要输入的五个元素,仅此而已 - 这可能是一个无效的假设,但对我来说似乎是一个不错的选择)。


In your first one, you have (comments added to explain what's happening):在您的第一个中,您有(添加评论以解释正在发生的事情):

int input,n;       // n has arbitrary value.
cin >> n;          // overwritten with 5.
vector<int> v(n);  // initial vector has five zeros.

This means your vector will be created with five (assuming you input that for n ) default-constructed entries.这意味着您的向量将由五个(假设您为n输入)默认构造的条目创建。 Then you go and insert five more, which is why you get five zeros followed by your actual data.然后你 go 并再插入五个,这就是为什么你得到五个零,后面跟着你的实际数据。


Your second one has:你的第二个有:

int input,n;       // n has arbitrary value.
vector<int> v(n);  // initial vector of that arbitrary value (probably).
cin >> n;          // overwritten with five but it's too late anyway.

This is actually undefined behaviour since you have no idea what n will be when you create a vector of that size.这实际上是未定义的行为,因为您不知道创建该大小的向量时n将是什么。 In your case, it appears to be zero but that's just a lucky coincidence.在您的情况下,它似乎为零,但这只是一个幸运的巧合。

Note the use of the word "probably" above.请注意上面“可能”一词的使用。 While it's likely that the vector will be created with some arbitrary number of entries, undefined behaviour means exactly that - you should not rely on it at all.虽然很可能会使用任意数量的条目创建向量,但未定义的行为恰恰意味着 - 您根本不应该依赖它。 By rights, it could quite easily delete all your files while playing derisive_laughter.wav through your sound system:-)按理说,它可以很容易地在通过您的音响系统播放derisive_laughter.wav时删除您的所有文件:-)

The most likely case however will be one of:然而,最可能的情况将是以下情况之一:

  • it'll work as you thought, because n is zero;它会像你想象的那样工作,因为n为零;
  • it'll have some arbitrary number of zeros at the start, similar to your first code snippet;它在开始时会有一些任意数量的零,类似于您的第一个代码片段;
  • it'll cause an out-of-memory exception because n is ridiculously large;它将导致内存不足异常,因为n大得离谱; or或者
  • it'll cause an exception because n is negative.它会导致异常,因为n是负数。

In any case, you should generally either pre-allocate and just set the already-existing values:无论如何,您通常应该预先分配并设置已经存在的值:

vector<int> v(2);
v[0] = 7;
v[1] = 42;

or not pre-allocate, using push_back to expand the vector on demand:预分配,使用push_back按需扩展向量:

vector<int> v;
v.push_back(7);
v.push_back(42);

Hope, one can write the 1st case of writing program in the question in the following way to get correct result希望,可以通过以下方式编写问题中编写程序的第一种情况以获得正确的结果

1
2
3
4
5
5 

for the input given as in the question对于问题中给出的输入

#include <iostream>
#include <vector>
using namespace std;

int main()
{
   int n;
   cin>>n;
   vector<int> v(n);

   for(int i=0;i<n;i++){
       cin>>v.at(i);           
   }
   for(int i=0;i<v.size();i++){
       cout << v[i] << endl;
   }
   cout << v.size() << endl;
   return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM