STL facility for copying array of objects?

F

Frederick Gotham

I want to copy an array of objects.

If it were C, I'd simply do:

memcpy( target_array, source_array, sizeof target_array );


However, this won't suffice for objects in C++.


Does the C++ STL provide a facility for copying arrays of objects, or must
I write my own loop?
 
I

Ian Collins

Frederick said:
I want to copy an array of objects.

If it were C, I'd simply do:

memcpy( target_array, source_array, sizeof target_array );


However, this won't suffice for objects in C++.


Does the C++ STL provide a facility for copying arrays of objects, or must
I write my own loop?
Look for constructors that take a pair of iterators.
 
J

Jerry Coffin

[ ... ]
Does the C++ STL provide a facility for copying arrays of objects, or must
I write my own loop?

std::copy will work with arrays of objects, just as it does with
other collections.
 
F

Frederick Gotham

Frederick Gotham posted:
I want to copy an array of objects.

If it were C, I'd simply do:

memcpy( target_array, source_array, sizeof target_array );


However, this won't suffice for objects in C++.


Does the C++ STL provide a facility for copying arrays of objects, or
must I write my own loop?


I'll be more specific.

I want to copy-construct an entire array of objects, rather than default-
construct a new array and subsequently perform assignments.

It would be great if the STL provided such a facility. At the moment, I
have to use the following code... but it's inefficient if I end up
dealing with something simple like an array of short's.


/* The following function takes the address of a raw
byte buffer, in which it shall construct the new
array */


template<class T>
T *CopyConstructArray( unsigned char * const p_target,
const T *p_source,
unsigned long len )
{

unsigned char *p = p_target;

do
{
new(p) T(*p_source);
} while ( p += sizeof(T), ++p_source, --len );


return reinterpret_cast<T*>(p_target);
}


template<class T>
void ArbitraryFunc( const T *p_start, const T *p_over )
{
unsigned long const len = p_over - p_start;

unsigned char * const p_buf = new unsigned char[ sizeof(T) * len ];

T *p_array = CopyConstructArray( p_buf, p_start, len);

/* Now p_array points to the copy-constructed array */


/* More code */
}



int main()
{
std::string array[64];

ArbitraryFunc( array, *(&array + 1) );
}
 
A

Alf P. Steinbach

* Frederick Gotham:
Frederick Gotham posted:

You mean the C++ standard library. A large part of the STL is part of
the standard library. But they're two different things.

I'll be more specific.

I want to copy-construct an entire array of objects, rather than default-
construct a new array and subsequently perform assignments.

Use std::vector.
 
T

Tom Widmer

Frederick said:
I want to copy an array of objects.

If it were C, I'd simply do:

memcpy( target_array, source_array, sizeof target_array );


However, this won't suffice for objects in C++.


Does the C++ STL provide a facility for copying arrays of objects, or must
I write my own loop?

std::uninitialized_copy covers part of what you want.

Tom
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top