Using one vector to construct another

C

Chris Roth

I am constructing a vector of objects of class A. The constructor for
class A takes an instance of class B in its constructor. I have a vector
of B objects and would like to construct a vector of A objects so that
each is constructed using the corresponding B object.

Is this possible or do I need to use pointers along with new and delete?

Thanks
 
D

darrylsh at hotmail

I am constructing a vector of objects of class A. The constructor for
class A takes an instance of class B in its constructor. I have a vector
of B objects and would like to construct a vector of A objects so that
each is constructed using the corresponding B object.

Is this possible or do I need to use pointers along with new and delete?

Thanks

vector vect_of_A;
for (size_t i = 0; i < vect_of_B.size();i++)
{
vect_of_a.push_back(A(vect_of_B));
}
 
S

Salt_Peter

I am constructing a vector of objects of class A. The constructor for
class A takes an instance of class B in its constructor. I have a vector
of B objects and would like to construct a vector of A objects so that
each is constructed using the corresponding B object.

Is this possible or do I need to use pointers along with new and delete?

Thanks

With std::back_inserter...

#include <iostream>
#include <vector>
#include <algorithm>

class B { };

class A {
B m_b;
public:
A(const B& b) : m_b( b ) { }
};

int main()
{
std::vector< B > vb(10);
std::vector< A > va;
// reserve here
std::copy( vb.begin(), vb.end(), std::back_inserter(va) );
}
 
S

Salt_Peter

I am constructing a vector of objects of class A. The constructor for
class A takes an instance of class B in its constructor. I have a vector
of B objects and would like to construct a vector of A objects so that
each is constructed using the corresponding B object.

Is this possible or do I need to use pointers along with new and delete?

Thanks

With std::back_inserter...

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

class B { };

class A {
B m_b;
public:
A(const B& b) : m_b( b ) { }
};

int main()
{
std::vector< B > vb(10);
std::vector< A > va;
std::copy( vb.begin(), vb.end(), std::back_inserter(va) );
}
 
G

Gianni Mariani

Chris said:
I am constructing a vector of objects of class A. The constructor for
class A takes an instance of class B in its constructor. I have a vector
of B objects and would like to construct a vector of A objects so that
each is constructed using the corresponding B object.

Is this possible or do I need to use pointers along with new and delete?


Any iterator whose dereferenced type can be assigned to the type in the
vector can use this format:

std::vector<B> b;
....
std::vector<A> a( b.begin(), b.end() );

or

a.assign( b.begin(), b.end() );
 
M

Markus Schoder

With std::back_inserter...

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

class B { };

class A {
B m_b;
public:
A(const B& b) : m_b( b ) { }
};

int main()
{
std::vector< B > vb(10);
std::vector< A > va;
std::copy( vb.begin(), vb.end(), std::back_inserter(va) );
}

This of course works only if A's constructor is not explicit.
 
J

James Kanze

This of course works only if A's constructor is not explicit.

If A's constructor is not explicit, then:

std::vector< A > va( vb.begin(), vb.end() ) ;

is all that is needed. If A's constructor is explicit, then you
probably need transform and a transforming object, or some sort
of transforming iterator. With transform:

struct XForm
{
A operator()( B const& b ) const
{
return static_cast< A >( b ) ;
}
} ;

std::transform( vb.begin(), vb.end(),
XForm(),
std::back_inserter( va ) ) ;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top