STL algorithm算法lexicographical_compare(30)
lexicographical_compare原型:
std::lexicographical_compare
default (1) |
template <class InputIterator1, class InputIterator2> |
---|---|
custom (2) |
template <class InputIterator1, class InputIterator2, class Compare> |
该函数是依照字典序測试[frist1,last1)是否小于[first2,last2).
字典序是指依照字母在字典中出现的顺序。
该函数使用opeartor<或者是comp进行比較。
假设两个序列长度不同,而且短序列和长序列头部全然一样,比如example和examplee.那么,长度大的字典序比短序的大。
其行为类似于:
|
|
一个简单的样例:
#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
——————————————————————————————————————————————————————————————————
STL algorithm算法lexicographical_compare(30)的更多相关文章
- STL algorithm算法lower_bound和upper_bound(31)
lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...
- STL algorithm算法merge(34)
merge原型: std::merge default (1) template <class InputIterator1, class InputIterator2, class Outpu ...
- STL algorithm算法mismatch(37)
mismatch原型: std::mismatch equality (1) template <class InputIterator1, class InputIterator2> p ...
- STL algorithm算法is_permutation(27)
is_permutation原型: std::is_permutation equality (1) template <class ForwardIterator1, class Forwar ...
- STL algorithm算法minmax,minmax_element(36)
minmax原型: std::minmax C++11 C++14 default (1) template <class T> pair <const T&,const T ...
- STL algorithm算法min,min_element(35)
min样板: std::min C++98 C++11 C++14 default (1) template <class T> const T& min (const T& ...
- STL algorithm算法max,max_elements(33)
max原型: std::max C++98 C++11 C++14 default (1) template <class T> const T& max (const T& ...
- STL algorithm算法mov,move_backward(38)
move原型: std::move template <class InputIterator, class OutputIterator> OutputIterator move (In ...
- STL algorithm算法make_heap和sort_heap(32)
make_heap原型: std::make_heap default (1) template <class RandomAccessIterator> void make_heap ( ...
随机推荐
- 安装spark1.3.1单机环境 分类: B8_SPARK 2015-04-27 14:52 1873人阅读 评论(0) 收藏
本文介绍安装spark单机环境的方法,可用于测试及开发.主要分成以下4部分: (1)环境准备 (2)安装scala (3)安装spark (4)验证安装情况 1.环境准备 (1)配套软件版本要求:Sp ...
- percona-toolkit源码编译安装
安装依赖软件yum install perl-ExtUtils-CBuilder perl-ExtUtils-MakeMakeryum install perl-Time-HiRes perl-DB ...
- [TypeStyle] Use TypeStyle keyframes to create CSS animations
We cover CSS keyframes and how to create them using TypeStyle. We then show how to use the keyframes ...
- [PostgreSQL] Ensure Uniqueness in Postgres
Let’s say we have a bank. Our bank wants to give each account for each user a unique name, for insta ...
- Android 最火高速开发框架AndroidAnnotations简单介绍
在上一篇Android 最火的高速开发框架androidannotations配置具体解释中介绍了在eclipse中配置androidannotation的步骤,如需配置请參考. 1.目标 andro ...
- mariadb远程不能访问,出现Can't connect to MySQL server on '' (10061)
一,现象: 1. 1 远程连接数据库mariadb时,报错 二,定位: 2. 1 首先本地连接上数据库,然后操作权限表数据 ,然后远程再次连接依然连接不上: 2. 2 搜索mariadb的配置文 ...
- 反向代理:是指以代理server来接收Internet上的请求,然后将请求转发到内部网络的server上,并将结果返回给Internet上连接的client,此时的代理server对外就表现为反向代理server。
Nginx安装好之后.開始使用它来简单实现反向代理与负载均衡的功能.在这之前.首先得脑补一下什么是反向代理和负载均衡. 反向代理:是指以代理server来接收Internet上的请求,然后将 ...
- mysql去除字段内容的空格和换行回车
MySQL 去除字段中的换行和回车符 解决方法: UPDATE tablename SET field = REPLACE(REPLACE(field, CHAR(10), ''), ...
- AOP概述:
AOP可以在不修改源代码的情况下,对程序进行增强. AOP面向切面进行编程,Spring将AOP引入到框架中,但是也需要遵守AOP联盟的规范. 通过预编译的方式和运行期动态代理实现程序功能的同意维护的 ...
- ArcGIS Engine中正确释放打开资源
转自原文 ArcGIS Engine中正确释放打开资源 AE中对MDB,SDE等数据库操作时,打开后却往往不能及时释放资源,导致别人操作提示对象被锁定. 很多帖子说了很多原理,看的也烦且不实用,比如一 ...