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.
Printable View
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.
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.
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.
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.