Created
March 1, 2012 13:40
-
-
Save loosechainsaw/1949884 to your computer and use it in GitHub Desktop.
C++ Array Merge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename T> | |
void array_merge(T* first1, T* first2, T* second1, T* second2, T* result){ | |
bool first_longest = (first2 -1 - first1) > (second2 -1 - second1); | |
while((first1 != first2) && (second1 != second2)){ | |
if(*first1 < *second1) | |
*result = *first1++; | |
else | |
*result = *second1++; | |
++result; | |
} | |
if(first_longest) | |
std::copy(first1,first2,result); | |
else | |
std::copy(second1,second2,result); | |
} | |
void array_merge_exp(bool run){ | |
if(!run) return; | |
int a[] = {1,2,3,4,5,6}; | |
int b[] = {2,3,3,10,11,12,17}; | |
int c[13]; | |
array_merge(a, &a[6], b, &b[7], c); | |
cout << "Printing items" << endl; | |
for(int i = 0; i < 12; i++){ | |
cout << c[i] << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment