lexicographical_compare

[機能]

  2 つの系列をアルファベット順に比較し,先の系列が次の系列より辞書的に前にあれば true を返します.2 引数の比較関数を指定することも可能です(下の表現).

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

	template <class InputIterator1, class InputIterator2> bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
	template <class InputIterator1, class InputIterator2, class Compare> bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp);
		

[使用例]

  1. lexicographical_compare の使用方法です.
    #include <stdio.h>
    #include <vector>
    #include <algorithm>
    #include <functional>
    using namespace std;
    
    bool less_a(int a, int b)
    {
    	return (bool)((a%10 < b%10) ? 1 : 0);
    };
    
    class less_b : public binary_function<int, int, bool>
    {
    	public:
    		result_type operator() (first_argument_type a, second_argument_type b)
    		{
    			return (result_type)((a%10 < b%10) ? 1 : 0);
    		}
    };
    
    int main()
    {
    	bool b;
    	int i1;
    	vector<int> v1, v2, v3;
    	vector<int>::iterator it;
    					// 初期設定
    	printf("**初期状態 v1**\n");
    	for (i1 = 0; i1 < 5; i1++)
    		v1.push_back(10+i1);
    	for (it = v1.begin(); it != v1.end(); it++)
    		printf("  %d", *it);
    	printf("\n");
    
    	printf("**初期状態 v2**\n");
    	for (i1 = 0; i1 < 5; i1++)
    		v2.push_back(11+i1);
    	for (it = v2.begin(); it != v2.end(); it++)
    		printf("  %d", *it);
    	printf("\n");
    
    	printf("**初期状態 v3**\n");
    	for (i1 = 0; i1 < 5; i1++)
    		v3.push_back(12+i1);
    	for (it = v3.begin(); it != v3.end(); it++)
    		printf("  %d", *it);
    	printf("\n");
    					// v1 と v2
    	b = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end(), less_a);
    							// 以下の方法でも可能
    //	b = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end(), less_b());
    	if (b)
    		printf("v1 は v2 より前\n");
    	else
    		printf("v1 は v2 より後\n");
    					// v3 と v2
    	b = lexicographical_compare(v3.begin(), v3.end(), v2.begin(), v2.end(), less_a);
    	if (b)
    		printf("v3 は v2 より前\n");
    	else
    		printf("v3 は v2 より後\n");
    
    	return 0;
    }
    
    (出力)
    
    **初期状態 v1**
      10  11  12  13  14
    **初期状態 v2**
      11  12  13  14  15
    **初期状態 v3**
      12  13  14  15  16
    v1 は v2 より前
    v3 は v2 より後
    			
[参照]

equal

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