user119653
user119653

Reputation:

c++ array value matching variable

I would like to see if a value equals any of the values in an array. like this

Beginning of function(here I give Val differnt values with for loops...)

       for (i=0;i<array_size;i++)
            if (Val==array[i])
            do something

    else 
    do something else if non of the above values matched val...

If none of the Arrayvalues matches my val I would like to do something else, but only once... And if it matches then I would like to go back to the beginning of the function that will give me a different value to val... Where should I put in this code

Thx, for a great forum /Buxley

Upvotes: 1

Views: 10468

Answers (6)

Loki Astari
Loki Astari

Reputation: 264381

Please look up what the STL can do for you.
There is a whole section on algorithms:

if (std::find(array,array+array_size,Val) != array+array_size)
{
    // Found it.
}
else
{
    // Did not find it.
}

Upvotes: 2

ovanes
ovanes

Reputation: 5673

Sorry to intervene. I have some feeling, that you were going to ask smth. else. As I understand you, you have an ordered set of values and would like to find the first possible value from that set in an array.

If so use the find_first_of algorithm from the C++ standard library. Algorithms in STL might be better optimized for your compiler (e.g. they might support parallel search).

Here is an adapted code taken from CPPReference.com. You are not limited to int-values.

int nums[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int* nend = &nums[10];

int targets[] = { 9, 4, 7 };
int* tend=&targets[3];

using namespace std;
int* result = find_first_of( &nums[0], nend, &targets[0], tend );

if( result == nend )
  cout << "Did not find any of { 9, 4, 7 }" << endl;
else
  cout << "Found a matching target: " << *result << endl;

After the value found result points to the element in the nums array which matches first possible element of the targets array elements. If none matched result equals nend.

Best Regards,
Ovanes

Upvotes: 2

xtofl
xtofl

Reputation: 41509

For most lookup actions, the STL has an algorithm. Finding a value in an array can be done using ... std::find.

const char values[] = "abcdefg";
const char* values_end = values + _countof( values );

const bool dPresent = 
 std::find_if( values, values_end , 'd' ) != values_end ;

if( dPresent ) {
    ...
}

Upvotes: 3

sharptooth
sharptooth

Reputation: 170489

Use a flag to indicate whether the condition was satisfied at least once:

bool hasMatch = false;
for (i=0;i< array_size;i++) {
        if (Val==array[i]) {
            //       do something
            hasMatch = true;
        }
}
if( !hasMatch ) {
  // do whatever else
}

This will invoke the "do something" for every matching element. If you want to invoke it for the first matching element only use break; after "do something".

Upvotes: 3

Otto Allmendinger
Otto Allmendinger

Reputation: 28268

you can use a find function

int find(int value, int* array, int size) {
    int i;
    for (i=0; i<size; i++) {
        if (array[i]==value) {
           return i;
        }
    }
    return -1;
}

Now you can do

if (find(value, array, array_size)>=0)) {
   do_something_when_found();
} else {
   do_something_when_not_found();
}

Upvotes: 3

ralphtheninja
ralphtheninja

Reputation: 133008

Not entirely sure what you're after. You can always do your stuff if you find a match and then just return from the function.

for (int i=0; i < array_size; i++) {
    if (Val==array[i]) {
        // do something
        return;
    }
}

// if we end up here, there was no match
// do something else ..

Or, you could set a boolean value and break the loop instead, there are many ways. Pick one :)

Upvotes: 1

Related Questions