Article ID: 156302 - Last Review: December 1, 2003 - Revision: 2.0

INFO: STL Sample for the find Function

This article was previously published under Q156302

On This Page

Expand all | Collapse all

SUMMARY

The sample code below illustrates how to use the count_if, begin, and end STL function in Visual C++.

MORE INFORMATION

Required Header

<algorithm>
				

Prototype

   template<class InputIterator, class T> inline
       InputIterator find(InputIterator first,
                          InputIterator last,
                          const T& value)
				
NOTE: The class/parameter names in the prototype do not match the version in the header file. Some have been modified to improve readability.

Description

The find algorithm locates the first element in the range [first, last) that matches value and returns the iterator positioned at the first matching element, or last + 1 if no such element exists.

Sample Code

////////////////////////////////////////////////////////////////////// 
// 
// Compile options needed: /GX
// 
// find.cpp : Illustrates how to use the find function.
// 
// Functions:
// 
//   find  - Find the first element in a sequence that matches value.
// 
// Written by Kalindi Sanghrajka
// of Microsoft Product Support Services,
// Software Core Developer Support.
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
////////////////////////////////////////////////////////////////////// 

// disable warning C4786: symbol greater than 255 character,
// okay to ignore
#pragma warning(disable: 4786)

#include <algorithm>
#include <iostream>
using namespace std;

void main()
{
    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
    cout << "IntArray { " ;
    for(i = 0; i < ARRAY_SIZE; i++)
        cout << IntArray[i] << ", " ;
    cout << " }" << endl ;

    // 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)  // matching element found
        cout << "First element that matches " << value
             << " is at location " << location - IntArray << endl;
    else                                    // no matching element was
                                            // found
        cout << "The sequence does not contain any elements"
             << " with value " << value << endl ;
    }
				
Output:

IntArray { 1, 2, 3, 4, 4, 5, 6, 7, }

First element that matches 4 is at location 3

REFERENCES

Visual C++ Books On Line: Visual C++ Books:C/C++:Standard C++ Library Reference.

APPLIES TO
  • The Standard C++ Library, when used with:
    • Microsoft Visual C++ 4.2 Professional Edition
    • Microsoft Visual C++ 4.2 Enterprise Edition
    • Microsoft Visual C++ 5.0 Learning Edition
Keywords: 
kbcode kbinfo KB156302
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
 

Article Translations