Skip to main content
gamedev.net
🔒 Locked

Array Concatenation

Started by HanSoL0 10 replies 700+ views
Original Post
HanSoL0
HanSoL0
Let's say I have one big array:
int bigArray[60];
And six smaller arrays:
int littleArray0[10];
int littleArray1[10];
int littleArray2[10];
int littleArray3[10];
int littleArray4[10];
int littleArray5[10];
I would like to copy the the contents of littleArray0 into the first 10 slots of bigArray, then concatenate all the other littleArray's at the end of the bigArray. Is there an easy way to do this in C++?
Aprosenf
Aprosenf
int *arrayPointers[6] = {littleArray0, littleArray1, littleArray2, littleArray3, littleArray4, littleArray5}; int i; for(i = 0; i < 6; i++)	memcpy(bigArray + 10 * i, arrayPointers, 10 * sizeof(int));
Chris Hare
Chris Hare
Quote:
Original post by Anonymous Poster
Yes, stl.


6 statements, to be exact.
GnuVince
GnuVince
Quote:
Original post by HanSoL0
Thanks so much, guys. Sometimes my brain just doesn't want to do the simple stuff. :P


Seeing how it has to be done, I can't blame you, it is not simple at all. I use higher level languages, and it'd go like this:

bigArray = larray1 + larray2 + larray3 + larray4 + larray5 + larray6


snk_kid
snk_kid
Here is one possiable solution with very little code needed to write:

#include <algorithm>#include <vector>int main() {   std::vector< std::vector<int> > little_arrays(6, std::vector<int>(10));   //yada, yada do some stuff like putting values in   std::vector<int> big_array(60);    for(int i = 0; i < little_arrays.size(); ++i) {      std::copy(little_arrays.begin(), little_arrays.end(), (big_array.begin() + (i * 10)));   }   return 0;}


But it means getting rid of static arrays for standard library vector which is generally preferable considering your using C++ and have limited stack memory.
DigitalDelusion
DigitalDelusion
Quote:
Original post by GnuVince
Seeing how it has to be done, I can't blame you, it is not simple at all. I use higher level languages, and it'd go like this:

bigArray = larray1 + larray2 + larray3 + larray4 + larray5 + larray6


Actually the STL isn't that much different, at least not if you've done some functional programming :)

#include <iostream>#include <iterator>#define END(x) (x + sizeof(x)/sizeof(*x))int main(void){	int BigArray[12];	int Small1[2] = { 1, 1};	int Small2[2] = { 2, 2};	int Small3[2] = { 3, 3};	int Small4[2] = { 4, 4};	int Small5[2] = { 5, 5};	int Small6[2] = { 6, 6};	std::copy( Small6, END(Small6), std::copy( Small5, END(Small5), std::copy( Small4, END(Small4),	std::copy( Small3, END(Small3), std::copy( Small2, END(Small2),	std::copy( Small1, END(Small1), BigArray))))));	std::copy( BigArray, END(BigArray), std::ostream_iterator<int>(std::cout));}


will happily give you the output: "112233445566"
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
amedlock
amedlock
For comparison here is concatenation in D

int[10] littleArray0;int[10] littleArray1;int[10] littleArray2;int[10] littleArray3;int[10] littleArray4;int[10] littleArray5;int[10] littleArray6;int[] bigArray ;bigArray ~= littleArray0 ~ littleArray1 ~ littleArray2            littleArray3 ~ littleArray4 ~ littleArray5            ~ littleArray6;
DigitalDelusion
DigitalDelusion
nothings stopping you from attaining almost that syntax in C++ while using vectors I could hack it togheter just to prove the point, the real question is if array concatenation is common enough to warrant it.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
DigitalDelusion
DigitalDelusion
As I can hear the excitment and anticipation build here's a quick hack that displays how todo it:
#include <iostream>#include <iterator>#include <vector>template <typename T, typename A>struct vector_concat{	typedef std::vector<const std::vector<T,A>*> vector_t;	mutable vector_t vectors;		vector_concat(const std::vector<T,A> &a, const std::vector<T> &b)	{		vectors.push_back( &a);		vectors.push_back( &b);	}	operator std::vector<T,A>() const	{		std::vector<T,A>v;		for(typename vector_t::const_iterator itr = vectors.begin(); itr != vectors.end(); ++itr)			v.insert( v.end(), (*itr)->begin(), (*itr)->end());		return v;	}};template <typename T, typename A>vector_concat<T,A> operator+(const std::vector<T,A> &a, const std::vector<T,A> &b){ return vector_concat<T,A>( a, b);}template <typename T, typename A>const vector_concat<T,A>& operator+(const vector_concat<T,A>& c, const std::vector<T,A>& v){	c.vectors.push_back( &v);	return c;}int main(void){	std::vector<int> small1;	std::vector<int> small2;	std::vector<int> small3;	small1.push_back( 1);	small1.push_back( 1);	small2.push_back( 2);	small2.push_back( 2);	small3.push_back( 3);	small3.push_back( 3);	std::vector<int> big = small1 + small2 + small3;	std::copy( big.begin(), big.end(), std::ostream_iterator<int>(std::cout));}


GCC and VC7 friendly.

for those that won't be bothred to read the whole thing the important line is this:
std::vector<int> big = small1 + small2 + small3;


and it simply concatenates the vectors...
C++ is powerfull enough to let you express that within the language, now how's that for flexible?
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.