Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++比较多个int数组和最小跨度返回数组_C++_Arrays - Fatal编程技术网

C++比较多个int数组和最小跨度返回数组

C++比较多个int数组和最小跨度返回数组,c++,arrays,C++,Arrays,我正在处理一个问题,其中我有多个数组,可以与单个数组进行比较。将返回索引间隔最短的数组 下面是我将使用的一组阵列的示例: 如果重要,这些值代表RGB值 int[] workingset = {55, 34,102}; int[] test1 = {54,36,99};` int[] test2 = {21,65,231}; int[] test3 = {76,35,1}; int[] test4 = {133,22,3}; 因为test1[]值最接近workingset[

我正在处理一个问题,其中我有多个数组,可以与单个数组进行比较。将返回索引间隔最短的数组

下面是我将使用的一组阵列的示例:

如果重要,这些值代表RGB值

int[] workingset = {55, 34,102};
int[] test1 = {54,36,99};`   
int[] test2 = {21,65,231};    
int[] test3 = {76,35,1};    
int[] test4 = {133,22,3};
因为test1[]值最接近workingset[],所以test1[]将是返回的数组


*很抱歉,我没有提供示例代码,但我实在想不出一种方法来拼凑这个难题。

索引之间最短跨度的度量标准是什么?我猜你是想最小化两个数组之间差值的绝对值之和

定义度量后,请尝试以下伪代码:

min_metric = MAX_INT
min_array = null
for array in arrays:
    if array.length() == workingset.length():
        metric = calculateMetric(workingset, array)
        if metric < min_metric:
            min_metric = metric
            min_array = array

你可以很容易地把所有的r,g,b元素加起来,然后检查哪一个元素的差别最小

#include <iostream>

int main(int argc, char* args[])
{
    int workingset [] = {55, 34,102};
    int test1 [] = {54,36,99};  
    int test2 []= {21,65,231};    
    int test3 [] = {76,35,1};    
    int test4 [] = {133,22,3};


    int sums [4] = {};
    for(int i=0; i<3;++i){
        sums[0] += abs(test1[i]-workingset[i]);
    }
    std::cout << "diff test1 " << sums[0] << std::endl;

    for(int i=0; i<3;++i){
        sums[1] += abs(test2[i]-workingset[i]);
    }
    std::cout << "diff test2 " << sums[1] << std::endl;

    for(int i=0; i<3;++i){
        sums[2] += abs(test3[i]-workingset[i]);
    }
    std::cout << "diff test3 " << sums[2] << std::endl;

    for(int i=0; i<3;++i){
        sums[3] += abs(test4[i]-workingset[i]);
    }   
    std::cout << "diff test4 " << sums[3] << std::endl;

    int smallestIndex = 0;
    int smallestDiff = INT_MAX;
    for(int i=0; i< 4; i++){
        if(sums[i] < smallestDiff){
            smallestIndex = i;
            smallestDiff = sums[i];
        }
    }

    std::cout << "array with smallest index: " << smallestIndex << std::endl;
    return 0;
}

我编辑了microsoft特定的包含和数据类型。

让我猜一下。假设您正在尝试编写颜色匹配程序。考虑这些数组向量。然后,workingset和testX之间向量差的绝对长度将是要使用的度量

或在代码中:

int [] delta = { 0, 0, 0 };
for (int i = 0; i < delta.length; ++i) delta[i] = workingset[i] - testX[i];
double metric = 0;
for (int x: delta) metric += x * x;
metric = sqrt(metric); // and use this to assess how far away the testX color is from the workingset color (sqrt operation is optional)

您的意思是将阵列与参考阵列单元逐个进行比较,然后选择总体杂散最低的阵列吗?在这种情况下,您也应该选择一个距离函数。不,遗憾的是,它必须找到所有数组索引之间的最短跨度平均值。问题是什么?如何返回数组?如何计算最短跨度平均值?如何从基于距离函数的阵列选择中选择阵列?什么是包括stdafx.h?什么是??什么是??对于非Microsoft特定的问题,不应给出Microsoft特定的答案。这些只是visual studio的默认导入。你可以用任何需要的东西来代替它。因为他没有要求一个特定的环境,我只是提出了我自己的标准材料。不过,我编辑了我的答案。谢谢你,我刚刚运行了你的代码,一切正常!干杯来自@bobah的答案具有相同的效果。square和abs函数有着相同的意图。这听起来是一个很好的方法,我会在今天早上尝试,你知道这个方法是否比@StevenB的答案更准确?你能举个例子吗?