简体   繁体   中英

How to add a vector to a vector of vectors inside functions in c++

I tried navily to add a vector to a vector of vectors inside a function: the function gets a vector of vectors as a pointer (called r) and it adds to r another vector.

std::vector<double> b;

b.push_back(0);
b.push_back(0);
b.push_back(0);
b.push_back(v0*sin(theta));
b.push_back(0);
b.push_back(v0*cos(theta));
b.push_back(0);
cout<<"befor"<<endl;
(*r).push_back(b);
cout<<"after"<<endl;

but the function crashes when it tries to add b to r. I believe it's because b is an internal object, but why the programe crashes when it tries to add b and not after (when it ends the function) ? to check this I added a print line "before" and "after" (it prints "before" but not "after" and then it crashes).

Can anyone explane me what it the best way to add a vectors to a vector of vectors inside a function?

The fully function looks like this:

 void solve_all(double v0,double theta,double omega,double phi,double 
 dt_in,double total_time,std::string solver, double eps_in, 
  std::vector<std::vector<double>>* r)
  {
   double delta,eta,miu,diff,time,eps_f,dt_f,eps_t;
   std::vector<double> r_1(7),r_2(7),r_temp(7);
   int i,good;
   mone_f=0;
std::vector<double> b;

b.push_back(0);
b.push_back(0);
b.push_back(0);
b.push_back(v0*sin(theta));
b.push_back(0);
b.push_back(v0*cos(theta));
b.push_back(0);
cout<<"befor"<<endl;
(*r).push_back(b);
cout<<"after"<<endl;
i=1;
time=b[6];
dt_f=dt_in;
eps_f=eps_in;
delta=eps_f*dt_f/(total_time-time);
eta=delta/4;
miu=delta/2;
eps_t=1e-10;

while(total_time-time>eps_t)
{
    cout<<time<<endl;
    good=0;
    while(good==0)
    {
        if(dt_f>total_time-time){dt_f=total_time-time;}
        solve(&b,&r_1,dt_f,omega,phi,solver);
        solve(&b,&r_temp,dt_f/2,omega,phi,solver);
        solve(&r_temp,&r_2,dt_f/2,omega,phi,solver);
        diff=0; 
        for(int j=0; j<=5; j++)
        {
            double subs;
            subs=r_2[j]-r_1[j];
            if(subs<0){subs=subs*(-1);}
            if(subs>diff){diff=subs;}
        }
        if(diff>delta)
        {
            dt_f=dt_f/2;
        }
        else
        {
            if(miu<diff&&diff<delta)
            {
                good=1;
                dt_f=0.9*dt_f;
            }
            else
            {
                if(eta<diff&&diff<miu)
                {
                    good=1;
                }
                else
                {
                    if(eta>diff)
                    {
                        good=1;
                        dt_f=1.1*dt_f;
                    }
                }
            }
        }
        i++;
        b.clear();
        if(solver.compare("RK2"))
        {
            for(int j=0; j<=6;i++)
            {
                b.push_back((4*r_2[j]-r_1[j])/3);
            }
        }

        if(solver.compare("RK3"))
        {
            for(int j=0; j<=6;i++)
            {
                b.push_back((8*r_2[j]-r_1[j])/7);
            }
        }
        (*r).push_back(b);
        eps_f=eps_f-diff;
        time=b[6];
        delta=eps_f*dt_f/(total_time-time);
        eta=delta/4;
        miu=delta/2;
    }
}



}

related functions:

 double big_f(double v)
 {
     return (0.0039+0.0058/(1+exp((v-35)/5)));
 }



 double f1(double vx,double vy,double vz,double omega ,double phi)
 {
     double v;
     mone_f++;
     v=sqrt(pow(vx,2)+pow(vy,2)+pow(vz,2));
     return (-big_f(v)*v*vx+B*omega*(vz*sin(phi)-vy*cos(phi)));
 }



 double f2(double vx,double vy,double vz,double omega ,double phi)
 {
     double v;
     mone_f++;
     v=sqrt(pow(vx,2)+pow(vy,2)+pow(vz,2));
     return (-big_f(v)*v*vy+B*omega*(vx*cos(phi)));
 }


  double f3(double vx,double vy,double vz,double omega ,double phi)
  {
     double v;
     mone_f++;
     v=sqrt(pow(vx,2)+pow(vy,2)+pow(vz,2));
    return (-g-big_f(v)*v*vz-B*omega*(vx*sin(phi)));
  }

 void solve(std::vector<double>* r,std::vector<double>* r_new,double 
 l,double omega, double phi, std::string solver)
 {
    double vx,vy,vz,vx2,vy2,vz2,vx3,vy3,vz3;
   if (solver.compare("RK2")==0)
    {
       vx=(*r)[3];
       vy=(*r)[4];
      vz=(*r)[5];
       vx2=vx+0.5*l*f1(vx,vy,vz,omega,phi);
       vy2=vy+0.5*l*f2(vx,vy,vz,omega,phi);
       vz2=vz+0.5*l*f3(vx,vy,vz,omega,phi);
       (*r_new).push_back((*r)[0]+l*vx2);
       (*r_new).push_back((*r)[1]+l*vy2);
       (*r_new).push_back((*r)[2]+l*vz2);
       (*r_new).push_back((*r)[3]+l*f1(vx2,vy2,vz2,omega,phi));
       (*r_new).push_back((*r)[4]+l*f2(vx2,vy2,vz2,omega,phi));
       (*r_new).push_back((*r)[5]+l*f3(vx2,vy2,vz2,omega,phi));
       (*r_new).push_back((*r)[6]+l);
   }
   if(solver.compare("RK3")==0)
   {
       vx=(*r)[3];
       vy=(*r)[4];
       vz=(*r)[5];
       vx2=vx+0.5*l*f1(vx,vy,vz,omega,phi);
       vy2=vy+0.5*l*f2(vx,vy,vz,omega,phi);
       vz2=vz+0.5*l*f3(vx,vy,vz,omega,phi);
       vx3=vx+2*l*f1(vx2,vy2,vz2,omega,phi)-l*f1(vx,vy,vz,omega,phi);
       vy3=vy+2*l*f2(vx2,vy2,vz2,omega,phi)-l*f2(vx,vy,vz,omega,phi);
       vz3=vz+2*l*f3(vx2,vy2,vz2,omega,phi)-l*f3(vx,vy,vz,omega,phi);
       (*r_new).push_back((*r)[0]+l*(vx+4*vx2+vx3)/6);
       (*r_new).push_back((*r)[1]+l*(vx+4*vx2+vx3)/6);
       (*r_new).push_back((*r)[2]+l*(vx+4*vx2+vx3)/6);
       (*r_new).push_back((*r)[3]+l(f1(vx,vy,vz,omega,phi)+4*f1(vx2,vy2,vz2,omega,phi)+f1(vx3,vy3,vz3,omega,phi))/6);
    (*r_new).push_back((*r)[4]+l*(f2(vx,vy,vz,omega,phi)+4*f2(vx2,vy2,vz2,omega,phi)+f2(vx3,vy3,vz3,omega,phi))/6);
    (*r_new).push_back((*r)[5]+l*(f3(vx,vy,vz,omega,phi)+4*f3(vx2,vy2,vz2,omega,phi)+f3(vx3,vy3,vz3,omega,phi))/6);
    (*r_new).push_back((*r)[6]+l);
}
}

If you take a reference std::vector<std::vector<double>>& or pointer std::vector<std::vector<double>>* you can push_back as many and any std::vector<double> you want. It doesn't matter if you're pushing back stack variables, push_back creates a copy.

Your problem is probably that your r pointer is invalid. Try checking that it isn't a nullptr , or if it was pointing to a stack variable that fell out of scope - in this case it does matter. Alternatively, and preferably, change your function definition to take a reference and see if the compiler complains.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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