Tutorial :Does C++ have a sequential search function?



Question:

I have a small unsorted array and I'd like to find the index of a particular value. Does C++ have a built-in sequential search function for this, or do you just write the loop yourself each time it comes up?

I'm specifically using a C-style array like:

std::string arr[5] = { "EVEN", "ODD", "NONE", "MARK", "SPACE" };  

and I need the index of a value that the user supplies.


Solution:1

std::find() should work:

#include <stdio.h>  #include <algorithm>  #include <string>    using std::string;    std::string arr[5] = { "EVEN", "ODD", "NONE", "MARK", "SPACE" };      int main() {        string* pArrEnd = arr + sizeof( arr)/sizeof(arr[0]);        string* pFound = std::find( arr, pArrEnd, "MARK");        if (pFound == pArrEnd) {          printf( "not found\n");      }      else {          printf( "%s was found at index %d\n", pFound->c_str(), pFound - arr);          printf( "or using STL: %d\n", std::distance( arr, pFound));      }        return 0;  }  


Solution:2

Use std::find() from the STL-algorithm-library, or the find()-method of your particular container.


Solution:3

You can use STL algos on containers other than just STL containers. For example, you can std::find() in a C-style array:

// alloc the array  static const size_t numItems = 100000;  int * items = new int[numItems];    // fill the array  for( size_t n = 0; n < numItems; ++n )      items[n] = n;    // find 42 using std::find()  int* found = std::find(&items[0], &items[numItems], 42);  if( found == &items[numItems] )  {      // this is one past the end, so 42 was not found      items[0] = 42;  }  else  {      // we found the first instance of 42 at this location      // change it to 43      *found = 43;  }  


Solution:4

I suppose you need the index and not the iterator.

int main()  {      // for c++ vector      typedef int Element;      typedef std::vector<Element> CppVector;        CppVector v;      v.push_back( 2 );      v.push_back( 4 );      v.push_back( 8 );      v.push_back( 6 );        const Element el = 4;        CppVector::const_iterator it = std::find( v.begin(), v.end(), el );      if ( it == v.end() )      {          std::cout << "there is no such element" << std::endl;      }      else      {          const CppVector::size_type index = it - v.begin();          std::cout << "index = " << index << std::endl;      }        // for C array      typedef Element CVector[4];      CVector cv;      cv[0] = 2;      cv[1] = 4;      cv[2] = 8;      cv[3] = 6;        const std::size_t cvSize = sizeof( cv ) / sizeof( Element );        std::cout << "c vector size = " << cvSize << std::endl;        const Element* cit = std::find( cv, cv + cvSize, el );      const std::size_t index = cit - cv;        if ( index >= cvSize )          std::cout << "there is no such element" << std::endl;      else          std::cout << "index = " << index << std::endl;  }  


Solution:5

In addition to the STL possibility (std::find) already mentioned there is the POSIX function lsearch (with c semantics).


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »