|
-
March 22nd, 2009, 02:17 PM
#1
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;
}
-
March 22nd, 2009, 02:24 PM
#2
Re: Searching for value in array
Never mind, i found it!
Its like this: if ( a [i][j] == (char)80 )
Last edited by MasterDucky; March 24th, 2009 at 12:31 PM.
-
March 22nd, 2009, 05:28 PM
#3
Re: Searching for value in array
 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.
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
March 23rd, 2009, 02:48 AM
#4
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.
-
March 23rd, 2009, 03:32 AM
#5
Re: Searching for value in array
You can use a pointer to look for the item
-
March 23rd, 2009, 03:48 AM
#6
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);
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
March 23rd, 2009, 03:49 AM
#7
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;
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
March 23rd, 2009, 04:01 AM
#8
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).
-
March 23rd, 2009, 04:30 PM
#9
Re: Searching for value in array
 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;
}
-
March 23rd, 2009, 04:53 PM
#10
Re: Searching for value in array
 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);
-
March 24th, 2009, 03:35 AM
#11
Re: Searching for value in array
Cheers, that makes sense.
I didn't actually try my example out 
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.
Last edited by JohnW@Wessex; March 24th, 2009 at 04:53 AM.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
March 24th, 2009, 01:55 PM
#12
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?
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.
-
March 24th, 2009, 02:10 PM
#13
Re: Searching for value in array
 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.
 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.
-
March 24th, 2009, 02:44 PM
#14
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.
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?
-
March 24th, 2009, 02:55 PM
#15
Re: Searching for value in array
 Originally Posted by MasterDucky
That sounds strange. How come we dont know a simple thing like that.
Because it depends on the implementation.
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|