Searching an array?

This is a discussion on Searching an array? within the C++ Programming forums, part of the General Programming Boards category; Is there a built in function that will search for all occurances of a letter or number in an array? ...

  1. #1
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280

    Wink Searching an array?

    Is there a built in function that will search for all occurances of a letter or number in an array? I know I could create one using a for loop but is there anything that will already do this?

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    not to my knowledge...

    however, you can come close with a combination of functions:
    Code:
    vector<string> array;
    
    for (vector<string>::iterator i = array.begin(); i != array.end(); ++i)
      if ((*i).find("K",0) != string::npos)
        ;  //do whatever in the conditition that the match has been found

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    There is std::find you pass an iterator(pointer) to the start and one past the end, it returns the first element found or the pointer to one past the end if not found.

    int arr[] ={2,5,3,4,9}
    int *p = std::find(arr,&arr[sizeof(arr)],4);
    p==&arr[3];
    p = std::find(arr,&arr[sizeof(arr)],8);
    p==&arr[sizeof(arr)]

    vectors and strings provide faster and more usefull specializations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 01:20 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  5. searching thru a c++ class array
    By stanleyw in forum C++ Programming
    Replies: 1
    Last Post: 05-29-2002, 10:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21