在STL中实现LexicoGraphical_Compare的C ++程序
C++函数std::algorithm::lexicographical_compare()测试一个范围是否在字典上小于另一个范围。字典比较是一种比较,通常用于在字典中按字母顺序对单词进行排序。
声明
模板<classInputIterator1,classInputIterator2>
boollexicographical_compare(InputIterator1first1,InputIterator1last1,InputIterator2first2,InputIterator2last2);
算法
Begin
result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end())
if (result == true)
Print v1 is less than v2
result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end())
if (result == false)
Print v1 is not less than v2
End示例
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int main(void) {
//v1和v2的初始化
vector<string> v1 = {"One", "Two", "Three"};
vector<string> v2 = {"one", "two", "three"};
bool result;
result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end());
if (result == true)
cout << "v1 is less than v2." << endl;
v1[0] = "two";
result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end());
if (result == false)
cout << "v1 is not less than v2." << endl;
return 0;
}输出结果
v1 is less than v2. v1 is not less than v2.
热门推荐
10 小红书生日祝福语简短
11 小朋友祝福语简短的话
12 企业励志拜年祝福语简短
13 老婆生日祝福语简短孩子
14 要过年了祝福语简短
15 新年恋人祝福语简短创意
16 工厂祝福语简短10字
17 结婚祝福语亲姐姐简短
18 韩语贺卡祝福语简短版