equal

[機能]

  first1 から last1 で指定した範囲が,first2 で指定された要素列と等しい(上の形式),または,二項関数オブジェクトで指定した意味で等しい(下の形式)場合に true を返します.

[形式]
	#include <algorithm>
	#include <functional>

	template <class InputIterator1, class InputIterator2> bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
	template <class InputIterator1, class InputIterator2, class BinaryPredicate> bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred);
		

[使用例]

  1. equal の使用方法です.
    #include <stdio.h>
    #include <math.h>
    #include <vector>
    #include <algorithm>
    #include <functional>
    using namespace std;
    					// 誤差の範囲で同じか否かのチェック
    class Equal : public binary_function<double, double, bool>
    {
    	public:
    		result_type operator() (first_argument_type a, second_argument_type b)
    		{
    			return (result_type)((fabs(a-b) < 0.1) ? 1 : 0);
    		}
    };
    
    int main()
    {
    	bool b;
    	vector<double> v1, v2;
    	vector<double>::iterator it;
    					// 初期設定
    	printf("**初期状態 v1**\n");
    	v1.push_back(2.99);
    	v1.push_back(5.02);
    	v1.push_back(4.0);
    	v1.push_back(4.95);
    	v1.push_back(4.05);
    	for (it = v1.begin(); it != v1.end(); it++)
    		printf("  %f", *it);
    	printf("\n");
    
    	printf("**初期状態 v2**\n");
    	v2.push_back(3.0);
    	v2.push_back(5.0);
    	v2.push_back(4.0);
    	v2.push_back(5.0);
    	v2.push_back(4.0);
    	for (it = v2.begin(); it != v2.end(); it++)
    		printf("  %f", *it);
    	printf("\n");
    					// 検索
    	b = equal(v1.begin(), v1.end(), v2.begin());
    	if (b)
    		printf("2 つのシーケンスは等しい\n");
    	else
    		printf("2 つのシーケンスは等しくない\n");
    
    	b = equal(v1.begin(), v1.end(), v2.begin(), Equal());
    	if (b)
    		printf("2 つのシーケンスは等しい\n");
    	else
    		printf("2 つのシーケンスは等しくない\n");
    
    	return 0;
    }
    
    (出力)
    
    **初期状態 v1**
      2.990000  5.020000  4.000000  4.950000  4.050000
    **初期状態 v2**
      3.000000  5.000000  4.000000  5.000000  4.000000
    2 つのシーケンスは等しくない
    2 つのシーケンスは等しい
    			
[参照]

mismatchlexicographical_compare

ホームページ 目次 演習解答例目次 付録目次 索引