C++ STL中的词典学

C++ STL为解决基本的公共生活问题提供了许多实用工具。比较值总是必要的,但有时我们也需要比较字符串。因此 使用字典编纂的_compare() 比较字符串 .

null

字典中常用的是按字母顺序排列单词;它需要将两个范围内具有相同位置的元素连续地相互比较,直到一个元素不等于另一个元素。词典比较是比较这些第一个不匹配成分的结果。此函数在中定义 头球。

它有以下两种实现:

语法1:

lexicographical_compare(iter1 beg1, iter1 end1, iter2 beg2, iter2 end2) 

模板:

template 
  bool lexicographical_compare(iter1 beg1, iter1 end1, 
                               iter2 beg2, iter2 end2)
{
  while (beg1!=end1)
  {
    if (beg2==end2 || *beg2<*beg1) return false;
    else if (*beg1<*beg2) return true;
    ++beg1; ++beg2;
  }
  return (beg2!=end2);
}

参数:

  • beg1: 将迭代器输入到第一个序列的初始位置。
  • (完) 将迭代器输入到第一个序列的最终位置。
  • beg2: 将迭代器输入到第二个序列的初始位置。
  • (完) 将迭代器输入到第二个序列的最终位置。

返回值: 如果range1在字典上严格小于range2,则返回布尔值true,否则返回false。

例子:

CPP

// C++ code to demonstrate the working of
// lexicographical_compare(), implementation 1
#include <algorithm>
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// initializing char arrays
char one[] = "geeksforgeeks" ;
char two[] = "gfg" ;
// using lexicographical_compare for checking
// is "one" is less than "two"
if (lexicographical_compare(one, one + 13, two,
two + 3))
cout << "geeksforgeeks is lexicographically less "
"than gfg" ;
else
cout << "geeksforgeeks is not lexicographically "
"less than gfg" ;
}


输出

geeksforgeeks is lexicographically less than gfg

语法2:

lexicographical_compare(iter1 beg1, iter1 end1, iter2 beg2, iter2 end2, Compare comp) 

模板:

template 
  bool lexicographical_compare(iter1 beg1, iter1 end1, 
                               iter2 beg2, iter2 end2)
{
  while (beg1!=end1)
  {
    if (beg2==end2 || *beg2<*beg1) return false;
    else if (*beg1<*beg2) return true;
    ++beg1; ++beg2;
  }
  return (beg2!=end2);
}

参数:

  • beg1: 将迭代器输入到第一个序列的初始位置。
  • (完) 将迭代器输入到第一个序列的最终位置。
  • beg2: 将迭代器输入到第二个序列的初始位置。
  • (完) 将迭代器输入到第二个序列的最终位置。
  • 公司: 比较器函数,返回每个被比较元素的布尔真/假。此函数接受两个参数。它可以是函数指针或函数对象,不能更改值。

返回值: 如果range1在字典上严格小于range2,则返回布尔值true,否则返回false。

例子:

CPP

// C++ code to demonstrate the working of
// lexicographical_compare()
#include <algorithm>
#include <iostream>
using namespace std;
// helper function to convert all into lower case:
bool comp( char s1, char s2)
{
return tolower (s1) < tolower (s2);
}
// Driver Code
int main()
{
// initializing char arrays
char one[] = "geeksforgeeks" ;
char two[] = "Gfg" ;
// using lexicographical_compare for checking
// is "one" is less than "two"
// returns false as "g" has larger ASCII value than "G"
if (lexicographical_compare(one, one + 13, two,
two + 3))
cout << "geeksforgeeks is lexicographically less "
"than Gfg" ;
else
cout << "geeksforgeeks is not lexicographically "
"less than Gfg" ;
// using lexicographical_compare for checking
// is "one" is less than "two"
// returns true this time as all converted into
// lowercase
if (lexicographical_compare(one, one + 13, two, two + 3,
comp)) {
cout << "geeksforgeeks is lexicographically less " ;
cout << "than Gfg( case-insensitive )" ;
}
else {
cout << "geeksforgeeks is not lexicographically "
"less " ;
cout << "than Gfg( case-insensitive )" ;
}
}


输出

geeksforgeeks is not lexicographically less than Gfg
geeksforgeeks is lexicographically less than Gfg( case-insensitive )

申请: 比较字符串通常可以用于 词典 ,我们需要按字典顺序排列单词。这方面的一个例子是,在给定的一组单词中,找出字典中第一个出现的单词。

CPP

// C++ code to demonstrate the application of
// lexicographical_compare()
#include <bits/stdc++.h>
using namespace std;
// Driver Code
int main()
{
// initializing char arrays
char list[][100] = { { 'a' , 'b' , 'a' , 'c' , 'u' , 's' },
{ 'a' , 'p' , 'p' , 'l' , 'e' },
{ 'c' , 'a' , 'r' },
{ 'a' , 'b' , 'b' , 'a' } };
char min[100] = "zzzzzz" ;
// using lexicographical_compare for checking
// the smallest
for ( int i = 0; i < 4; i++)
if (lexicographical_compare(
list[i], list[i] + strlen (list[i]), min,
min + strlen (min)))
strcpy (min, list[i]);
// prints "abacus"
cout << "The smallest string is : " ;
for ( int i = 0; min[i] != ' ' ; i++)
cout << min[i];
}


输出

The smallest string is : abacus

词典编纂_compare()中出现异常: 如果元素比较或迭代器上的操作引发异常,则会引发异常。如果参数无效,则会导致未定义的行为。

本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享