Mega Code Archive

 
Categories / C++ Tutorial / Vector
 

Find the first element in the range (first, last + 1) that matches value

#include <iostream> #include <algorithm> using namespace std ; int main(void)  {    const int ARRAY_SIZE = 8;    int IntArray[ARRAY_SIZE] = { 1, 2, 3, 4, 4, 5, 6, 7 };    int *location ;  //stores the position of the first matching element.    int i;    int value = 4;    // print content of IntArray    for (i = 0; i < ARRAY_SIZE; i++)      cout << IntArray[i] << ", ";    //Find the first element in the range (first, last + 1) that matches value.    location = find(IntArray, IntArray + ARRAY_SIZE, value);        //print the matching element if any was found    if (location != IntArray + ARRAY_SIZE)        cout << "First element that matches " << value << " is at location "      << (location - IntArray) << endl;    else                          cout << "The sequence does not contain any elements with value " << value << endl; }