push_back single element in a vector of struct

Hello,i have a vector of struct, so defined
1
2
3
4
5
6
struct Point{
  int x,y;
  Point(int x,int y):x(x),y(y){};
}

vector<Point> pts;

now i want to insert element iteratively from another vector, so i'm doing this way
1
2
3
4
5
for(int i=0;i<another_vector.size();i++){
  pts.push_back(Point(0,0));
  pts[i].x=another_vector[i].x;
  pts[i].y=another_vecotr[i].y;
}


is this correct or is there a cheaper/more elegant way to do this?thanks
It is correct. But it would be more siimply to do the following way

1
2
3
4
for ( int i = 0; i < another_vector.size(), i++ )
{
   pts.push_back( another_vector[i] );
}


Or if your compiler supports new C++ standard then

for ( const auto &x : another_vector ) pts.push_back( x );

Or you can use standard algorithm std::copy

1
2
std::copy( another_vector.begin(), another_vector.end(),
                std::back_inserter( pts ) );
Simplest: pts.insert( pts.end(), another_vector.begin(), another_vector.end() ) ;
thanks vlad.But (i forgot before,my fault..sorry) if i have more fields in another_vector?for example if another_vector is also a struct like
1
2
3
struct AnotherStruct{
  int l,m,n,o;
}


and i only want for example l and m, or l and n, then how can i choose with your methods only the fields i'm interested in?
It would be even more simply to write :)


vector<Point> pts( another_vector.begin(), another_vector.end() );
> It would be even more simply to write :)

That won't be inserting into pts, it would be creating pts as a copy.
For that, this would do: vector<Point> pts = another_vector ;


> if i have more fields in another_vector?for example if another_vector is also a struct like ...
> and i only want for example l and m, or l and n

This is one way to do it:
1
2
3
4
5
6
7
8
9
10
11
struct Point
{
  int x,y;
  Point(int x,int y):x(x),y(y){};
};

struct AnotherStruct
{
  int l,m,n,o ;
  operator Point() const { return Point(l,n) ; }
};


And then:
1
2
3
4
std::vector<AnotherStruct> another_vector ;
std::vector<Point> pts ;
// ...
pts.insert( pts.end(), another_vector.begin(), another_vector.end() ) ;


Last edited on
In this case you can use standard algorithm std::transform.

1
2
3
4
5
6
7
8
pts.reserve( pts.size() + another_vector.size() );

std::transform( another_vector.begin(), another_vector.end(),
                       std::back_inserter( pts ),
                       [] ( const AnotherStruct &x )
                       {
                           return ( Point( x.l, x.m ) );
                       } );


Last edited on
ok thanks very much vlad and JLBorges!
A last question JLBorges,only for curiosity this time:if i have to choose l and m for one Point and l and o for another what should i change in your code for AnotherStruct?
> if i have to choose l and m for one Point and l and o for another
> what should i change in your code for AnotherStruct?

Something like this:
1
2
3
4
5
6
7
8
9
10
struct AnotherStruct
{
  int l,m,n,o ;
  
  operator Point() const 
  { 
      if( some_condition ) return Point( l, m ) ;
      else return Point( l, o ) ; 
  }
};


This kind of implicit conversion operator may not be a good design choice in many cases. Though it really depends on your specific context.
ok thank you!
Topic archived. No new replies allowed.