时间:2010-07-23 20:01:17

标签: c++ algorithm stl

8 个答案:

答案 0 :(得分:8)

答案 1 :(得分:2)

答案 2 :(得分:2)

我在互联网上看到了一些合并两个排序数组的解决方案,但其中大多数都非常繁琐。我改变了一些逻辑,以提供我能想出的最短版本:

void merge(const int list1[], int size1, const int list2[], int size2, int list3[]) {

    // Declaration & Initialization
    int index1 = 0, index2 = 0, index3 = 0;

    // Loop untill both arrays have reached their upper bound.
    while (index1 < size1 || index2 < size2) {

        // Make sure the first array hasn't reached 
        // its upper bound already and make sure we 
        // don't compare outside bounds of the second 
        // array.
        if ((list1[index1] <= list2[index2] && index1 < size1) || index2 >= size2) {
            list3[index3] = list1[index1];
            index1++;
        }
        else {
            list3[index3] = list2[index2];
            index2++;
        }
        index3++;
    }
}

答案 3 :(得分:1)

答案 4 :(得分:1)

答案 5 :(得分:1)

答案 6 :(得分:0)

答案 7 :(得分:-2)

相关问题