By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,508 Members | 1,685 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,508 IT Pros & Developers. It's quick & easy.

Using one vector to construct another

P: n/a
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
May 28 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On May 28, 4:28 pm, Chris Roth <czr...@mail.usask.cawrote:
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[i]));
}
May 28 '07 #2

P: n/a
On Mon, 28 May 2007 14:59:19 -0700, darrylsh at hotmail wrote:
On May 28, 4:28 pm, Chris Roth <czr...@mail.usask.cawrote:
>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;
Adding

vect_of_A.reserve(vect_of_B.size());

here will avoid reallocations.
for (size_t i = 0; i < vect_of_B.size();i++) {
vect_of_a.push_back(A(vect_of_B[i]));
}
--
Markus Schoder
May 28 '07 #3

P: n/a
Markus Schoder wrote:
On Mon, 28 May 2007 14:59:19 -0700, darrylsh at hotmail wrote:
>>On May 28, 4:28 pm, Chris Roth <czr...@mail.usask.cawrote:
>>>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;


Adding

vect_of_A.reserve(vect_of_B.size());

here will avoid reallocations.

>>for (size_t i = 0; i < vect_of_B.size();i++) {
vect_of_a.push_back(A(vect_of_B[i]));
}

Thanks to both.
May 28 '07 #4

P: n/a
On May 28, 5:28 pm, Chris Roth <czr...@mail.usask.cawrote:
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) );
}
May 28 '07 #5

P: n/a
On May 28, 5:28 pm, Chris Roth <czr...@mail.usask.cawrote:
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) );
}
May 28 '07 #6

P: n/a
Chris Roth wrote:
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() );
May 28 '07 #7

P: n/a
On Mon, 28 May 2007 15:46:01 -0700, Salt_Peter wrote:
On May 28, 5:28 pm, Chris Roth <czr...@mail.usask.cawrote:
>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) );
}
This of course works only if A's constructor is not explicit.

--
Markus Schoder
May 29 '07 #8

P: n/a
On May 29, 3:03 am, Markus Schoder <a3vr6dsg-use...@yahoo.dewrote:
On Mon, 28 May 2007 15:46:01 -0700, Salt_Peter wrote:
On May 28, 5:28 pm, Chris Roth <czr...@mail.usask.cawrote:
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?
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.
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 ) ) ;

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 29 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.