从两个或更多不同的动态数组中查找缺少的int值

时间:2018-01-05 11:08:38

标签: c++ arrays

例如,我有两个数组:

int array_1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int array_2[9] = {0, 3, 4, 6, 7, 2, 9, 8, 5};

如何在array_1中找到array_2中缺少的值?

编辑:这是我的SC。它只是缺少我有

的输出选项
using namespace std;

int main() {
    int n;
    array<int, 9> k;
    array<int, 10> b = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    cin >> n;

    for (int a = 0; a < n; a ++){ 
     for (int i = 0; i < 9; i++){
        cin >> k[i];
         }

      }
    return 0;
    }

1 个答案:

答案 0 :(得分:1)

您可以使用标准算法,即:

#include <iostream>
#include <algorithm>

int main() {
    int array_1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int array_2[9] = {0, 3, 4, 6, 7, 2, 9, 8, 5};
    int* missingValPtr = std::find_if(std::begin(array_1), std::end(array_1), [&](int arr1Val){
        return std::none_of(std::begin(array_2), std::end(array_2), [&](int arr2Val) {
            return arr1Val == arr2Val;
        }); 
    });
    if(missingValPtr != std::end(array_1)) {
        std::cout << "The missing vlue is: " << *missingValPtr;
    }
}