STL algorithm算法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:[email protected]

2014-9-17

于GDUT

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

时间: 2024-08-26 00:39:43

STL algorithm算法lexicographical_compare(30)的相关文章

STL algorithm算法集合

algorithm意为"演算法",是C++的标准模版库(STL)中最重要的头文件之一,提供了大量基于迭代器的非成员模版函数. 编程语言 C++ 类    别 C++标准库 头文件 #include <algorithm> 命名空间 using namespace std; 目录 ? 不修改内容的序列操作: ? 修改内容的序列操作: ? 划分操作: ? 排序操作: ? 二分法查找操作: ? 集合操作: ? 堆操作: ? 最大/最小操作: 不修改内容的序列操作: adjacen

STL algorithm算法rmonve,rmove_if(47)

rmove原型: std::remove template <class ForwardIterator, class T> ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val); 查找的得到第一个元素的位置,然后从此位置开始遍历容器,将后面的元素依次前移,跳过和value相同值的元素,也就是说,所有和value相同值的元素都会被覆盖,而其他的元素都会依次前移. 返回值是理论

STL algorithm算法generate和generate_n(22)

今后的stl算法部分就不贴cpluplus的原文了,简要的介绍为主. generate原型: std::generate template <class ForwardIterator, class Generator> void generate (ForwardIterator first, ForwardIterator last, Generator gen); 该函数是使用gen函数产生的值填充范围内元素的值. 其行为类似如下: 1 2 3 4 5 6 7 8 template &l

STL algorithm算法rotate,rotate_copy(51)

rotate原型: std::rotate C++98 C++11 template <class ForwardIterator> ForwardIterator rotate (ForwardIterator first, ForwardIterator middle, ForwardIterator last); 该函数是循环移位函数. 效果是交换[middle,last)和[first,middle)部分的位置. 具体实现请自行搜索. 类似实现为: template <class

STL algorithm算法search,search_n(52)

search原型: std::search equality (1) template <class ForwardIterator1, class ForwardIterator2> ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); predicate (2) template <class

STL algorithm算法pop_heap,push_heap(45)

pop_heap原型: std::pop_heap default (1) template <class RandomAccessIterator> void pop_heap (RandomAccessIterator first, RandomAccessIterator last); custom (2) template <class RandomAccessIterator, class Compare> void pop_heap (RandomAccessItera

STL algorithm算法lower_bound和upper_bound(31)

lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class ForwardIterator, class T> ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val); custom (2) template <class Forw

STL algorithm算法any_of译文及使用(3)

原文地址:http://www.cplusplus.com/reference/algorithm/any_of/ function template <algorithm> std::any_of template <class InputIterator, class UnaryPredicate> bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred); Test if any el

STL algorithm算法is_partitioned(26)

is_partitioned原型: std::is_partitioned template <class InputIterator, class UnaryPredicate> bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred); 测试范围内的元素是否是以pred为准则的一个划分.如果是,则返回true,否则返回false. 划分的意思是说,对每个元素进行pred(*it),得