Predicate Version of includes

在 Visual C++ 演示如何使用 包含 标准模板库函数的 (STL)谓词版本。

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

备注

备注

类/参数名在原型不匹配版本在头文件。修改某些提高可读性。

包含 算法搜索值的顺序值在另一个序列的。 包含 返回 true ,如果范围 [First2的每个元素。Last2) 在序列 [First1。Last1). 包含 此版本的假定使用 比较 功能,,两个序列排序。

示例

// includesPV.cpp 
// compile with: /EHsc
// 
// Illustrates how to use the predicate version of
// includes function.
//
// Functions:
//    includes - Search for one sequence in another.
//    string_compare - Compare strings, return true if
//                       s1 < s2.

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

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

using namespace std ;

bool string_compare(const string& s1, const string& s2)
{
    return s1 < s2 ? 1 : 0;
}
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, string_compare) ;
    sort(start2, end2, string_compare) ;

    // 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, string_compare) )
        cout << "CartoonVector includes CartoonDeque"
        << endl ;
    else
        cout << "CartoonVector does not include CartoonDeque"
        << endl ;

}

Output

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

CartoonDeque { Aladdin, Goofy, Jasmine,  }

CartoonVector includes CartoonDeque

要求

**标题:**algorithm

请参见

概念

标准模板库示例