Thread: Searching an array?

  1. #1

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