-
January 11th, 2009, 12:04 AM
#1
Can you make arrays equal to each other
Is there a way to make the elements of an array equal to the elements of another array?
I know that you can do it in a loop but I'm looking for something like a shortcut.
-
January 11th, 2009, 12:20 AM
#2
Re: Can you make arrays equal to each other
Well...depending on the type of the array. In case you are using STL containers then take a look at the following algorithms:
- copy (for unsorted ranges)
- merge (for sorted ranges)
- set_union (for sorted ranges - elements in both source ranges end up only once in destination range)
- set_intersection (for sorted ranges - only elements that are in both source ranges)
Edit: Okay....I guess, I actually misunderstood the question...if you want to make one array equal to the other, then the 'copy' algorithm is actually the one you are looking for (which can even be used with POD types). The other three are actually used for merging. Sorry for not paying attention in the first place and thanks to Lindley for giving me the wake up.
Last edited by Andreas Masur; January 11th, 2009 at 12:45 AM.
Reason: Brain cells were already sleeping....
-
January 11th, 2009, 12:29 AM
#3
Re: Can you make arrays equal to each other
In the event the array is of a plain-old-data type, you can always use memcpy(). std::copy() should be preferred, though, since it (probably) reduces to memcpy() in the POD case, while also correctly handling non-POD types.
-
January 11th, 2009, 12:47 AM
#4
Re: Can you make arrays equal to each other
Originally Posted by E-man96
Is there a way to make the elements of an array equal to the elements of another array?
I know that you can do it in a loop but I'm looking for something like a shortcut.
Code:
#include <algorithm>
SomeType array1[10];
SomeType array2[10];
//...
std::copy(array1, array1 + 10, array2); // set array2 equal to array1
Regards,
Paul McKenzie
-
January 11th, 2009, 02:18 AM
#5
Re: Can you make arrays equal to each other
Originally Posted by E-man96
Is there a way to make the elements of an array equal to the elements of another array?
I know that you can do it in a loop but I'm looking for something like a shortcut.
There's no built-in C++ language feature available (like field operations or so), so you must copy explicitly in one way or another.
Note that if you're doing extensive copying of large amounts of data, then you could consider using a fine-grain parallel library, like Intel TBB, to make it faster on multi-core processors.
Last edited by _uj; January 11th, 2009 at 02:25 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|