开发者

I can not figure out how to pass a vector back to main after it has been through the loop

开发者 https://www.devze.com 2023-02-11 04:18 出处:网络
#include <vector> #include <iostream> using namespace std; vector<int> fibonacci(int x, int y,vector<int> vi,int n){
#include <vector>

#include <iostream>

using namespace std;

vector<int> fibonacci(int x, int y,vector<int> vi,int n){

    vi[0]=x;
    vi[1]=y;

   for(int i=2;i<n;i++){

       x=vi[i-2];
       y=vi[i-1];
       vi[i]=x+y;

       cout<<vi[i]<<" "<<endl;

   }
return vi;

}

void printv(string label, vector<int> vi, int n){

   cout<<label<<" "<<endl;
   for (int j=0; j<n; j++)
       cout<<vi[j]<<" "<<endl;

}
/*void reverseV(vector<int> vi,int n)
{
    vector<int> rv= vi;
    for (int i=rv.size()-1; i>=0; i--) {
开发者_StackOverflow中文版            cout << rv[i] << endl;
        }*/
//}

int main()
{
    int x;
    int y;
    vector<int> vi;
    vector<int> rv;
   int n=100;
string label;
   cout<<"enter the max number of times you want it to add"<<endl;

   cin>>n;
cout<<"enter the first two fibonacci numbers"<<endl;
cin>>x>>y;
   fibonacci(x,y,vi,n);
 printv("vector: ",vi,n);
 printv("vector: ",rv,n);
 //vi.swap(rv);
}


You shouldn't even be passing a vector in, just create a new one and return that:

std::vector<int> fibonacci(int x, int y, int n)
{
    std::vector<int> vi(n);
    vi[0] = x;
    vi[1] = y;
    //etc...
    return vi;
}

Then, in main, you just need to capture this return value:

vi = fibonacci(x,y,n);


Your program contains several errors, for example:

  • std::vector already knows the number of contained elements, if you pass a vector there's no need to specify the size.

  • Elements in a vector must be allocated, you can't just assign to them with v[i]=x like you can do e.g. in javascript. Either you first give a size to the vector or you add elements with push_back.

  • Apparently you don't know the difference between passing by value and by reference.

But the biggest error is that you are trying to learn C++ by experimentation, and this is a suicidal approach for many reasons.

Much easier and better is to learn C++ by first reading a good book. You can find a list here.


You could pass your vector by reference rather than by value. To achieve this, change the declaration of your function to

void fibonacci(int x, int y,vector<int>& vi,int n){ ...

This way you are working on the same vector object inside the function, not just on a copy of it. Thus every change you make to the vector is visible outside the function too.


For the immediate question you just missed assigning the return value from your function back to vi.

rv = fibonacci(x,y,vi,n);

EDIT: There are a few other problems though, like you never actually allocated space on your vector, and you passed by value into the function AND returned it. Since you seem to want to not change the original in place, just use a local vector.

vector<int> fibonacci(int x, int y,int n){
    vector<int> vi(n);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号