Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C++ Programming

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

On May 28, 4:28 pm, Chris Roth <czr@mail.usask.ca> 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?

> 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]));

On Mon, 28 May 2007 14:59:19 -0700, darrylsh at hotmail wrote:
> On May 28, 4:28 pm, Chris Roth <czr@mail.usask.ca> 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?

>> 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

Thanks to both.
On May 28, 5:28 pm, Chris Roth <czr@mail.usask.ca> 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?

> 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) );

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() );

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

--
Markus Schoder

On May 29, 3:03 am, Markus Schoder <a3vr6dsg-use@yahoo.de> wrote:

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:james.ka@gmail.com
Conseils en informatique oriente objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc