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

STL facility for copying array of objects?

P: n/a

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?

--

Frederick Gotham
Jun 26 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Frederick Gotham wrote:
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.

--
Ian Collins.
Jun 26 '06 #2

P: n/a
In article <fw*******************@news.indigo.ie>, fg*******@SPAM.com
says...

[ ... ]
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 26 '06 #3

P: n/a
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) );
}

--

Frederick Gotham
Jun 26 '06 #4

P: n/a
* Frederick Gotham:
Frederick Gotham posted:
Does the C++ STL provide a facility for copying arrays of objects, or
must I write my own loop?

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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 26 '06 #5

P: n/a
Frederick Gotham wrote:
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
Jun 27 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.