Thread: About arrays

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    20

    About arrays

    Hello. A bit stupid question but sorry... I haven`t got much time to slove it. So... is there ant way to check is a value in array for example: if(a in b[]) {...} something like this...? THNX C ya
    Butterfly sweep can make torando in another side of world

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    There is no built in functionality. You can either write a function yourself using a simple for loop or you can check <algorithm> for a searching algorithm. Maybe there is one. However, if you have just an array of ints in the magnitude of 100 to 200 numbers or less, don't waste your energy and just use a small loop.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The <algorithm> header mentioned by nvoigt contains the find function that can be used with arrays as such:

    Code:
    #include <iostream>
    #include <algorithm>
    
    int main()
    {
        int array[10] = { 5, 8, 10, 12, 20, 16, 13, 15, 17, 4 };
        int *ptr;
    
        // Find the value 11
        ptr = std::find(array,array+10,11);
        if( ptr != array+10 )
            std::cout << "Found 11." << std::endl;
        else std::cout << "Could not find 11." << std::endl;
    
        // Find the value 12
        ptr = std::find(array,array+10,12);
        if( ptr != array+10 )
            std::cout << "Found 12." << std::endl;
        else std::cout << "Could not find 12." << std::endl;
    
        return 0;
    }
    Should output:
    Code:
    Could not find 11.
    Found 12.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM