CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    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;
    }

  2. #2
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    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.

  3. #3
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Searching for value in array

    Quote Originally Posted by MasterDucky View Post
    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

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Jan 2009
    Posts
    19

    Re: Searching for value in array

    You can use a pointer to look for the item

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    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

  7. #7
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    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

  8. #8
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    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).

  9. #9
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Searching for value in array

    Quote Originally Posted by Plasmator View Post
    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;
    }

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Searching for value in array

    Quote Originally Posted by JohnW@Wessex View Post
    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);

  11. #11
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    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

  12. #12
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    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.

  13. #13
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  14. #14
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    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
    Code:
     cout<< (char) i;
    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?

  15. #15
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured