次の方法で共有


find (STL Samples)

Visual C++ で 検索 の標準テンプレート ライブラリ関数を使用する方法に (STL) ついて説明します。

template<class InputIterator, class T> inline 
   InputIterator find(
      InputIterator First,
      InputIterator Last,
      const T& Value
   )

解説

[!メモ]

プロトタイプのクラスやパラメーター名はヘッダー ファイルのバージョンと一致しない。ただし読みやすさが向上するように変更されました。

検索 のアルゴリズムは範囲 [FirstLast) の最初の要素を検索 Value 探しそのような要素が存在しない場合反復子が最初に一致した要素の配置はを返します Last。

使用例

// find.cpp
// compile with: /EHsc

// Demonstrates using find() on a C++ array, a vector, and an STL array.

#include <algorithm>
#include <iostream>
#include <vector>
#include <array>

using namespace std;
using namespace std::tr1;

void FindInArray()
{
    const int ARRAY_SIZE = 8;
    int IntArray[ARRAY_SIZE] = { 1, 2, 3, 4, 4, 5, 6, 7 };

    int value = 4;

    // print contents of IntArray
    cout << endl << "array";
    for(int i = 0; i < ARRAY_SIZE; i++)
    {
        cout << " " << IntArray[i];
    }
    cout << endl;

    // Find the first element in the range [first, last)
    // that matches value.
    int *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;
    }
}

void FindInVector()
{
    vector<int> v;
    v.push_back( 1 );
    v.push_back( 2 );
    v.push_back( 3 );
    v.push_back( 4 );
    v.push_back( 4 );
    v.push_back( 5 );
    v.push_back( 6 );
    v.push_back( 7 );

    int value = 4;

    // print contents of v
    cout << endl << "vector";
    for(vector<int>::const_iterator i = v.begin(); i != v.end(); ++i)
    {
        cout << " " << *i;
    }
    cout << endl;

    // Find the first element in the range [first, last)
    // that matches value.
    vector<int>::const_iterator location = find(v.begin(), v.end(), value);

    //print the matching element if any was found
    if (location != v.end())         // matching element found
    {
        cout << "First element that matches " << value
             << " is at location " << location - v.begin() << endl;
    }
    else                             // no matching element was found
    {
        cout << "The sequence does not contain any elements"
             << " with value " << value << endl;
    }
}

void FindInStlArray()
{
    const int ARRAY_SIZE = 8;
    typedef array<int, ARRAY_SIZE> arraytype;
    arraytype a = { 1, 2, 3, 4, 4, 5, 6, 7 };

    int value = 4;

    // print contents of a
    cout << endl << "STL array";
    for(arraytype::const_iterator i = a.begin(); i != a.end(); ++i)
    {
        cout << " " << *i;
    }
    cout << endl;

    // Find the first element in the range [first, last)
    // that matches value.
    arraytype::const_iterator location = find(a.begin(), a.end(), value);

    //print the matching element if any was found
    if (location != a.end())         // matching element found
    {
        cout << "First element that matches " << value
             << " is at location " << location - a.begin() << endl;
    }
    else                             // no matching element was found
    {
        cout << "The sequence does not contain any elements"
             << " with value " << value << endl;
    }
}

int main() 
{
    FindInArray();
    FindInVector();
    FindInStlArray();
}

出力

array 1 2 3 4 4 5 6 7
First element that matches 4 is at location 3

vector 1 2 3 4 4 5 6 7
First element that matches 4 is at location 3

STL array 1 2 3 4 4 5 6 7
First element that matches 4 is at location 3

必要条件

ヘッダー : <algorithm>

参照

概念

標準テンプレート ライブラリのサンプル