-1

I am fairly new to C++ and I don't know how to define the function push_back to my struct:

struct Arco {

    int i, j;
    Arco () {};
    Arco (const Arco& obj): i(obj.i), j(obj.j) {};
    Arco(int _i, int _j) : i(_i), j(_j) {}   

};

I have a vector of vector of arcs:

vector < vector < Arco > > Df;
Df = vector < vector < Arco > > ( nn, vector < Arco > ( ) );

I want to be able to allow the command:

Df[i][j].push_back(Arco(u,v));

What do I suppose to do?

rbl
  • 17
  • 6

3 Answers3

1

You do not "define" a push_back, you use an existing one.

First, push back a vector into a vector of vectors, the push back an element into the inner vector.

df.push_back(vector<Arco>());
df[0].push_back(Arco(1, 2));
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Or `df.back().push_back(Arco(1, 2));` if you want to hit the one you just added regardless of how many were already there. – WhozCraig Aug 22 '15 at 22:22
  • But for each pair i-j, I'm suppose to have an arc (u,v). Didn't I need to make one push_back to each pair i-j? – rbl Aug 22 '15 at 22:26
0

Something like the following

vector<vector<Arco> > Df (nn, vector<Arco>());

//...

for ( std::vector<Arco> &inner : Df ) push_back( Arco( 10, 10 ) );

or

for ( std::vector<Arco> &inner : Df ) push_back( { 10, 10 } );

or

for ( std::vector<Arco> &inner : Df ) emplace_back( 10, 10 );

or

for ( std::vector<vector<Arco> >::size_type i = 0; i < DF.size(); i++ )
{
    DF[i].push_back( Arco( 10, 10 ) );
    //DF[i].push_back( { 10, 10 } );
    //DF[i].emplace_back( 10, 10 );
}

Or

for ( auto it = DF.begin(); it != DF.end(); ++it )
{
    it->push_back( Arco( 10, 10 ) );
    //it->push_back( { 10, 10 } );
    //it->emplace_back( 10, 10 );
}

As for expression

Df[i][j].push_back(Arco(u,v));

then it is incorrect because expression Df[i][j] is an object of type Arco not a vector.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • For each pair i-j, I'm suppose to have an arc associated. So, I thought that for each i-j, I was supposed to make a push_back, didn't I? – rbl Aug 22 '15 at 22:24
  • @Luiza Real No. Each i, j specify an object of typo Arco. So DF[i][j] is an object of type Arco while DF[i] is a vector of objects of type Arco std::vector. You should imagine your vector as a matrix. Each cell contains Arco not a vector. – Vlad from Moscow Aug 22 '15 at 22:26
  • So, how do I relate j with the arc (u,v)? Do I just need to write Df[i].push_back(Arco(u,v)), for each i, u and v? – rbl Aug 22 '15 at 22:36
  • @LuizaReal DF[i] is a vectors of type std::vector so to add elements to the row you should write DF[i].push_back( Arco( 10, 10 ) ); To change an already created element you can write DF[i][j] = Arco( 20, 20 ); – Vlad from Moscow Aug 22 '15 at 22:44
  • Sorry, but it still not clear. For each pair i-j (i is the origin and j is the destination), I have an arc u-v, which represents an arc in the shortest path form i to j. So, for each origin(i), and for each destination(j), I have an Df[i][j]. I can't see how i relate the destination (j) with the origin (i) and the arcs (u,v). – rbl Aug 22 '15 at 22:52
  • @Luiza Real If you have a matrix the pair i, j specify a cell in the matrix. At first you create an empty matrix std::vector> FD. It has neither row. You can create one row. FD.push_back( std::vector() ); Now thye matrix has one row. You can access it using FD[0]. But the row has no elements. You can create aell in the row writing FD[0].push_back( Arcp( 10, 10 ) ); Now you matrix has one ceell with coordinates FD[0][0]. – Vlad from Moscow Aug 23 '15 at 07:25
0

Sorry!

Couldn't I do:

for (int i = 0; i < ni; i++) {

  for (int j = 0; j < ni; j++) {

    for (int u = 0; u < nn; u++) {

      for (int v = 0; v < nn; v++) {

        Df[i][j].push_back(Arco(u,v));

      }

    }

  }

}
Bannings
  • 10,376
  • 7
  • 44
  • 54
rbl
  • 17
  • 6
  • Hi Luiza! The place to put stuff like this is either in the comments section or edited into your question. The answer section is reserved for complete answers only, thanks! – Maximillian Laumeister Aug 23 '15 at 00:22