我不知道,如果你想將它們合併爲交錯他們或只是連接兩個不同的陣列。如果它大步:
#include <iostream>
void mergeTwoArraysIntoOne(const int* lhs, const int* rhs, int* dst, size_t numElements) {
const int* const endLhs = lhs + numElements;
const int* const endRhs = rhs + numElements;
for (; lhs < endLhs ;) {
while (rhs < endRhs && *rhs < *lhs)
*(dst++) = *(rhs++);
*(dst++) = *(lhs++);
}
while (rhs < endRhs)
*(dst++) = *(rhs++);
}
void dumpArray(int* array, size_t elements) {
for (size_t i = 0; i < elements; ++i)
std::cout << array[i] << " ";
std::cout << std::endl;
}
int main() {
int array1[] = { 1, 2, 3 };
int array2[] = { 10, 20, 30 };
int array3[] = { 1, 11, 31 };
int result[6];
mergeTwoArraysIntoOne(array1, array2, result, 3);
dumpArray(result, 6);
mergeTwoArraysIntoOne(array2, array1, result, 3);
dumpArray(result, 6);
mergeTwoArraysIntoOne(array1, array3, result, 3);
dumpArray(result, 6);
mergeTwoArraysIntoOne(array3, array1, result, 3);
dumpArray(result, 6);
mergeTwoArraysIntoOne(array2, array3, result, 3);
dumpArray(result, 6);
mergeTwoArraysIntoOne(array3, array2, result, 3);
dumpArray(result, 6);
return 0;
}
現場演示:http://ideone.com/7ODqWD
如果它只是連接起來將:
std::copy(&lhs[0], &lhs[numElements], &dst[0]);
std::copy(&rhs[0], &rhs[numElements], &dst[numElements]);
歡迎堆棧溢出!感謝您發佈您的代碼,但請在您的問題中多加一點說明:您有什麼問題,您期望得到什麼結果,以及[您嘗試過什麼](http://whathaveyoutried.com)到目前爲止?通過[問題清單](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist)將幫助我們更好地回答你的問題。謝謝! –