Insert blank "nodes" in a vectors of structures

Hello,

I just declared an std::vector of an structure, like this,

1
2
3
4
5
6
7
8
struct Elem
   }
   std::string sub1;
   std::string sub2;
   std::string sub3;
   };

std::vector <Elem> Elements;


An I'm trying (without success) to insert blank Elements. I mean,

Supose four "nodes" Elements created: Elements[0] ... Elements[3],

How could I "insert" between Elements[1] and Elements[2], two more "nodes" Elements...

While in *simple* var types runs, doing

1
2
std::vector <std::string> sub1;
sub1.insert (sub1.begin()+2, 2, "hello");


So, I put,

Elements.insert (Elements.begin()+2, 2);

and g++ compiler throws me an error...

Could help ?

S.
1
2
3
4
std::vector<Elem>::iterator iter( sub1.begin() );
// ? assert( sub1.size() > 2 );
std::advance( iter, 2 );
sub1.insert( iter, 2, Elem() );

Thanks JSmith,

But maybe,

 
Elements.insert (Elements.begin()+2, 2, Elem());


works also (for the moment, it compiles, but the test is pending.).

In fact, I didn't reach to realize "Elem()" as third parameter.

Thanks a lot,

S.
If it compiles it will work.

std::advance() is more generic, since only random access iterator types allow scalar addition/subtraction.
Topic archived. No new replies allowed.