473,388 Members | 1,207 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Using one vector to construct another

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
8 1607
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Lasse Skyum | last post by:
I'm currently learning STL and I hate not knowing what is gooing on "inside" STL... not because I really _need_ to know it to develop my game project, but that's just my nature... like most of you...
2
by: The Directive | last post by:
I'm confused as to why C++ does not allow to create a vector of references (even though a vector of pointers can be created). I read some old threads about it but I still don't fully understand it....
5
by: Ahmad | last post by:
Hi all, I have written a simple c++ app, as I'm still learning c++. The thing works flawlessly on VC++6, but just doesn't work on g++. The transliterate function which causes the problem is...
15
by: Alex Vinokur | last post by:
I am looking for any custom allocator sample code for std::vector. Thanks. -- Alex Vinokur http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
17
by: Mark P | last post by:
Say I have objects of class C which are fairly large. Then consider: vector<C> vc; vc.push_back(C()); Naively this would seem to construct a temporary object C(), copy it into the space...
7
by: utab | last post by:
Dear all, I tried sth like this, compiles but segmentation fault error. In my reasoning field_values holds a vector<double> but when I tried, I understood that it is not the case :-). ...
7
by: hlg | last post by:
I have a question, which must surely have occurred to many programmers since STL first appeared, and yet I have found no reference to it anywhere, suggesting the problem is insoluble. Nevertheless,...
4
by: Peter Webb | last post by:
I am writing some visualisation code for 4 dimensional geometric shapes. This is to run in the C# .NET XNA environment, so I am cross posting a maths and C# group. My software runs just fine for...
0
by: tech | last post by:
Hi, I have developed a little class to help implement dynamic menus in our application which has a UI. This allows our engine to send a MenuItem object to the UI which is an observer of the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.