Searching for value in array
Is it possible to search for the value stored in an array?
Not the index of the array.
Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
char a[2][2]={{50,179},{80,110}};
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
if(*(a[i][j])=='80') // something like this
{
cout<<"80"<<endl;
}
else
{
cout<<"not found";
}
}
}
cout<<endl<<endl;
return 0;
}
Re: Searching for value in array
Never mind, i found it!
Its like this: if ( a [i][j] == (char)80 )
Re: Searching for value in array
Quote:
Originally Posted by
MasterDucky
Never mind, i found it!
Its like this: if((a[i][j]) == (char)80)
Just remember that when you use single quotes for a char, put only one digit inside (unless you're putting escape sequences, like '\n'). Otherwise, you're playing with some complicated C++ witchcraft best left buried.
Re: Searching for value in array
If these are supposed to be chars, why don't you say (example):
Code:
char a[2][2]={{'3', 'a'},{'F', 'u'}};
and then
Code:
if((a[i][j]) == 'F')
char is supposed to hold ASCII characters. If you want numbers use unsigned char, and if you want signed numbers use short or int.
Re: Searching for value in array
You can use a pointer to look for the item
Re: Searching for value in array
I'm pretty sure that even multi-dimensional arrays are contiguous.
In that case you could use std::find
Code:
char a[2][2]={{50,179},{80,110}};
bool found = std::find(a, a + 4, 80) != (a + 4);
Re: Searching for value in array
Or even
Code:
char a[2][2]={{50,179},{80,110}};
bool found = std::count(a, a + 4, 80) > 0;
Re: Searching for value in array
Excellent suggestion about standard algorithms, John. The only problem is that a won't decay properly. You'll need extra dereferences to make that work.
As a side note: OP, your assumptions about the range that plain old char can contain are dangerous (see *(*a + 1) in case of implicitly signed 8-bit bytes, for instance).
Re: Searching for value in array
Quote:
Originally Posted by
Plasmator
Excellent suggestion about standard algorithms, John. The only problem is that a won't decay properly. You'll need extra dereferences to make that work.
As a side note: OP, your assumptions about the range that plain old char can contain are dangerous (see *(*a + 1) in case of implicitly signed 8-bit bytes, for instance).
I recently learned this the hard way too and Paul straightened me out.
http://www.codeguru.com/forum/showth...=std%3A%3Afill
Here is a slight mod to John's example that worked for me. I didn't have time to come up with a way of doing it without the for loop. I'm guessing that the same problem would exist with std::find. You'd still need a loop to test each dimension.
Code:
void std_count_example_MDA()
{
char a[2][2]={{50,179},{80,110}};
bool found(false);
for(int i = 0; i < 2 && found == false; ++i)
{
found = std::count(a[i], a[i] + 2, 80) > 0;
}
std::cout << "found = " << found << std::endl;
}
Re: Searching for value in array
Quote:
Originally Posted by
JohnW@Wessex
I'm pretty sure that even multi-dimensional arrays are contiguous.
Yes---they're literally an array of arrays. However, they're laid out in memory exactly like a really big 1D array, so all you have to do is cast it so that sizeof(*a) gives you sizeof(char) rather than sizeof(char[2]).....
Code:
char a[2][2]={{50,179},{80,110}};
char *aptr = reinterpret_cast<char*>(a);
bool found = std::find(aptr, aptr + 4, 80) != (aptr + 4);
Re: Searching for value in array
Cheers, that makes sense.
I didn't actually try my example out :blush:
EDIT:
Actually this should be OK too.
Code:
char a[2][2]={{50,109},{80,110}};
char *aptr = &a[0][0];
bool found = std::find(aptr, aptr + 4, 80) != (aptr + 4);
I changed the 179 to 109 as 179 is too large for a char.
Re: Searching for value in array
Thanks for your precious help!
So would this or any other method showed by you would any advantage...
Code:
bool found = std::find(aptr, aptr + 4, 80) != (aptr + 4);
...over this one?
Code:
if ( a [i][j] == (char)80 )
Or are they only different ways to do the same thing?
Quote:
I changed the 179 to 109 as 179 is too large for a char.
What is the limit for a char? Isnt the same as ASCII?
I thought 255 was the limit for ASCII.
Re: Searching for value in array
Quote:
Originally Posted by MasterDucky
Or are they only different ways to do the same thing?
They are different ways to do the same thing, but generally it is better to use a generic algorithm unless a simple loop really is much clearer and simpler.
Quote:
Originally Posted by MasterDucky
What is the limit for a char?
It is guaranteed to be the same as the limit for signed char or unsigned char, depending on the implementation. The limit for signed char is guaranteed to be at least 127, while the limit for unsigned char is guaranteed to be at least 255. Since it is not known if char has the same range as signed char or unsigned char, one can suppose that 179 could be too large for a char.
Re: Searching for value in array
Thank you Laserlight.
So if i understand well if i wanna go until 255 normally i should declare an unsigned char.
Though if we do a looped until 255, it will print out all the printable characters.
Quote:
it is not known if char has the same range as signed char or unsigned char
That sounds strange. How come we dont know a simple thing like that.
Couldn't we test it?
Re: Searching for value in array
Quote:
Originally Posted by MasterDucky
That sounds strange. How come we dont know a simple thing like that.
Because it depends on the implementation.
Quote:
Originally Posted by =MasterDucky
Couldn't we test it?
You could use std::numeric_limits<char> and cast the result of min() and max() to int to find out the range.