开发者

How to set configuration parameters using an array table?

Following situation: I have an array with some constant values, which represent ranges.

A range is always between two values in the array, e.g.: 10 - 20 = range1

20-30 = range2 and so on...

const int arr[] = {10, 20, 30, 40, 50, 60};

With a Search function, I search for the number(val) between those ranges in arr[] and return the range index where val was found.

For example: if val = 15 → return value would be 1

if val = 33 → return value would be 3

int Search(const int arr[], int n, int val)
{
    int i = 0;

    while (i < n) {
        if (val > arr[i])
            ++i;
        else
            return i;
    }
    return -1;
}

开发者_如何学JAVAOK, this works out so far...

Now following problem: I have some parameters let's call them x, y, z which are simple integers and they depend on the value of val. The parameter values for x, y, z I know already before compilation, of course they are different for every range.

How can I now set x, y and z using the range index? How can I make an array for example with the constant parameter values for x, y, z and set them depending on the returned range index? Or should it be a struct? How would that look like...?

Thx


You could hold the parameters for each range in a struct:

struct range_parameters {
    int x;
    int y;
    // etc
}

And keep all these structs in a std::vector:

std::vector<range_parameters> params;

Adding the data would be done like this:

range_parameters params_for_range_1;
params_for_range_1.x = 1;
params_for_range_1.y = 2;

params[0] = params_for_range_1;

So finally you can access the parameters for range n as params[n-1].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜