简体   繁体   English

在 C++ 程序中使用向量不会打印任何内容

[英]Using vectors in C++ program is not printing anything

I have written a C++ program to find fibonacci numbers.我编写了一个 C++ 程序来查找斐波那契数。 It's running successfully but is not printing anything.它运行成功,但没有打印任何东西。

#include<bits/stdc++.h>

using namespace std;

int main(){
int n = 5;

vector<int> fib;

fib[0] = 0;
fib[1] = 1;


for (int i = 2; i <= n; i++){
    fib[i] = fib[i-1] + fib[i-2];
}

cout<<fib[n];
}

If I do the same thing using array instead of vector it prints successfully.如果我使用数组而不是向量做同样的事情,它会成功打印。

#include<bits/stdc++.h>

using namespace std;

int main(){
int n = 5;

int fib[10];

fib[0] = 0;
fib[1] = 1;


for (int i = 2; i <= n; i++){
    fib[i] = fib[i-1] + fib[i-2];
}

cout<<fib[n];
}

I have tested this on sublime text and onlinegdb.我已经在 sublime text 和 onlinegdb 上对此进行了测试。

int fib[10];

This creates an array of 10 integers.这将创建一个包含 10 个整数的数组。

vector<int> fib;

This creates a vector of size 0, with 0 integers.这将创建一个大小为 0 的向量,其中包含 0 个整数。

For these two snippets to match, you need to initialize the vector with 10 integers like the array.为了使这两个片段匹配,您需要像数组一样使用 10 个整数初始化向量。 So:所以:

vector<int> fib(10);

Some notes on vectors关于向量的一些注意事项

One of the big differences between std::vector and primitive arrays is that std::vector is resizable, So while initializing the vector with 10 ints like above will work, you could also add them on the fly using push_back() or by callingresize() . std::vector和原始 arrays 之间的最大区别之一是std::vector是可调整大小的,因此虽然像上面那样用 10 个整数初始化向量会起作用,但您也可以使用push_back()或通过调用即时添加它们resize()

Likewise, std::vector has a .at() function for access.同样, std::vector有一个 .at .at() function 用于访问。 It's marginally slower than the subscript operator ( [] ), so I would not suggest using it in production-level code.它比下标运算符 ( [] ) 稍慢,所以我不建议在生产级代码中使用它。 But while you're learning, I would strongly suggest using .at() , as it will do bounds checking for you.但是在您学习时,我强烈建议您使用.at() ,因为它会为您进行边界检查。 So this program would've told you you were trying to access locations in the vector that don't exist--instead of just running with Undefined Behavior.所以这个程序会告诉你你试图访问向量中不存在的位置——而不是仅仅以未定义的行为运行。

Note that when you wrote:请注意,当您编写时:

vector<int> fib;//creates an empty vector that is a vector of size 0

fib[0] = 0; //UNDEFINED BEHAVIOR
fib[1] = 1; //UNDEFINED BEHAVIOR

In the above code snippet, you created an empty std::vector .在上面的代码片段中,您创建了一个std::vector That is a vector of size 0 .那是一个大小0的向量。

Next when you wrote fib[0] = 0;接下来当你写fib[0] = 0; you're trying access the first element of the vector.您正在尝试访问向量的第一个元素。 That is, the element at index 0 .也就是说,索引为0的元素。 But note that there is no element inside the vector and so you have undefined behavior in your program.但请注意,向量内没有元素,因此您的程序中有未定义的行为

Similarly when you wrote fib[1] = 1;同样,当您编写fib[1] = 1; , you're trying to access the second element of the vector. ,您正在尝试访问向量的第二个元素。 But since there is no element inside the vector, this again leads to undefined behavior.但是由于向量内没有元素,这再次导致未定义的行为。

To solve this you can create a vector of some size(say 10) and then access its elements as shown below:为了解决这个问题,你可以创建一个一定大小(比如 10)的向量,然后访问它的元素,如下所示:

vector<int> fib(10);//create vector of size 10

fib[0] = 0;//OK NOW
fib[1] = 1;//OK NOW

Since you're using indices to access the elements of the vector, i have not suggested push_back .由于您使用索引来访问向量的元素,因此我没有建议push_back std::vector::push_back is a member function that can be used to add an element into the vector. std::vector::push_back是成员 function 可用于将元素添加到向量中。

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

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