May 2, 2012 at 7:02am UTC
Simplest: pts.insert( pts.end(), another_vector.begin(), another_vector.end() ) ;
May 2, 2012 at 7:05am UTC
It would be even more simply to write :)
vector<Point> pts( another_vector.begin(), another_vector.end() );
May 2, 2012 at 7:09am UTC
> 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 May 2, 2012 at 7:14am UTC
May 2, 2012 at 7:41am UTC
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 May 2, 2012 at 7:51am UTC
May 2, 2012 at 7:47am UTC
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?