STLalgorithm算法lexicographical_compare(30)


STLalgorithm算法lexicographical_compare(30)

lexicographical_compare原型:

std::lexicographical_compare

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

该函数是按照字典序测试[frist1,last1)是否小于[first2,last2).

字典序是指按照字母在字典中出现的顺序。

该函数使用opeartor<或者是comp进行比较。

如果两个序列长度不同,并且短序列和长序列头部完全一样,例如example和examplee.那么,长度大的字典序比短序的大。

其行为类似于:

1
2
3
4
5
6
7
8
9
10
11
12
template <class InputIterator1, class InputIterator2>
  bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1,
                                InputIterator2 first2, InputIterator2 last2)
{
  while (first1!=last1)
  {
    if (first2==last2 || *first2<*first1) return false;
    else if (*first1<*first2) return true;
    ++first1; ++first2;
  }
  return (first2!=last2);
}

一个简单的例子:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argv,char **argc)
{
	vector<char> v1{'h','e','l','l','o'};
	vector<char> v2{'h','e','l','l','o','o'};
	vector<char> v3{'h','e','l','m','o'};
	cout<<"v1=";
	for(char i:v1)
		cout<<i<<" ";
	cout<<endl;
	cout<<"v2=";
	for(char i:v2)
		cout<<i<<" ";
	cout<<endl;
	cout<<"v3=";
	for(char i:v3)
		cout<<i<<" ";
	cout<<endl;
	
	if(lexicographical_compare(v1.begin(),v1.end(),v2.begin(),v2.end()))
		cout<<"v1 is less than v2 "<<endl;
	else
		cout<<"v2 is less than v1 "<<endl;

	if(lexicographical_compare(v1.begin(),v1.end(),v3.begin(),v3.end()))
		cout<<"v1 is less than v3 "<<endl;
	else
		cout<<"v3 is less than v1 "<<endl;

}

运行截图:

该函数是否只能比较字母呢?答案是肯定的,不是!

因为对于任意的可以使用opeartor<进行比较的对象都可以使用该函数!

一个简单的例子:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argv,char **argc)
{
	vector<int> v1{1,2,3,4};
	vector<int> v2{1,2,3,4,5};
	vector<int> v3{1,2,3,3};
	cout<<"v1=";
	for(int i:v1)
		cout<<i<<" ";
	cout<<endl;
	cout<<"v2=";
	for(int i:v2)
		cout<<i<<" ";
	cout<<endl;
	cout<<"v3=";
	for(int i:v3)
		cout<<i<<" ";
	cout<<endl;
	
	if(lexicographical_compare(v1.begin(),v1.end(),v2.begin(),v2.end()))
		cout<<"v1 is less than v2 "<<endl;
	else
		cout<<"v2 is less than v1 "<<endl;

	if(lexicographical_compare(v1.begin(),v1.end(),v3.begin(),v3.end()))
		cout<<"v1 is less than v3 "<<endl;
	else
		cout<<"v3 is less than v1 "<<endl;

}

运行截图:

——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-17

于GDUT

——————————————————————————————————————————————————————————————————



发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注