次の方法で共有


includes (STL Samples)

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

template<class InputIterator1, class InputIterator2>
   inline bool includes(
      InputIterator1 First1,
      InputIterator1 Last1,
      InputIterator2 First2,
      InputIterator2 Last2
   )

解説

[!メモ]

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

含まれます。 アルゴリズムは値のシーケンスの値の 1 種類のシーケンスを検索します。 含まれます。true を返します。範囲 [First2 のすべての要素。Last2 は[First1 するようにスケジュールします)。Last1).このバージョンのは両方がシーケンス operator< を使用して並べ替えると仮定があります。

使用例

// includesP.cpp
// compile with: /EHsc

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

#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <vector>
#include <deque>

using namespace std;


int main()
{
    const int VECTOR_SIZE = 5 ;

    // Define a template class vector of strings
    typedef vector<string > StringVector ;

    //Define an iterator for template class vector of strings
    typedef StringVector::iterator StringVectorIt ;

    // Define a template class deque of strings
    typedef deque<string > StringDeque ;

    //Define an iterator for template class deque of strings
    typedef StringDeque::iterator StringDequeIt ;

    StringVector CartoonVector(VECTOR_SIZE) ;
    StringDeque CartoonDeque ;

    StringVectorIt start1, end1, it1 ;
    StringDequeIt start2, end2, it2 ;

    // Initialize vector Vector1
    CartoonVector[0] = "Aladdin" ;
    CartoonVector[1] = "Jasmine" ;
    CartoonVector[2] = "Mickey" ;
    CartoonVector[3] = "Minnie" ;
    CartoonVector[4] = "Goofy" ;

    start1 = CartoonVector.begin() ;  // location of first
                                      // element of CartoonVector

    end1 = CartoonVector.end() ;  // one past the location last
                                  // element of CartoonVector

    //Initialize list CartoonDeque
    CartoonDeque.push_back("Jasmine") ;
    CartoonDeque.push_back("Aladdin") ;
    CartoonDeque.push_back("Goofy") ;

    start2 = CartoonDeque.begin() ; // location of first
                                    // element of CartoonDeque

    end2 = CartoonDeque.end() ; // one past the location last
                                // element of CartoonDeque

    //sort CartoonVector and CartoonDeque alphabetically
    //includes requires the sequences
    //to be sorted.
    sort(start1, end1) ;
    sort(start2, end2) ;

    // print contents of CartoonVector and CartoonDeque
    cout << "CartoonVector { " ;
    for(it1 = start1; it1 != end1; it1++)
        cout << *it1 << ", " ;
    cout << " }\n" << endl ;
    cout << "CartoonDeque { " ;
    for(it2 = start2; it2 != end2; it2++)
        cout << *it2 << ", " ;
    cout << " }\n" << endl ;

    //Is CartoonDeque a subset of CartoonVector?
    if(includes(start1, end1, start2, end2) )
        cout << "CartoonVector includes CartoonDeque"
        << endl ;
    else
        cout << "CartoonVector does not include CartoonDeque"
        << endl ;

}

出力

CartoonVector { Aladdin, Goofy, Jasmine, Mickey, Minnie,  }

CartoonDeque { Aladdin, Goofy, Jasmine,  }

CartoonVector includes CartoonDeque

必要条件

ヘッダー : <algorithm>

参照

概念

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