#define _SCL_SECURE_NO_WARNINGS 1 // Turn off Microsoft Error/Warning
#include <algorithm>
#include <iostream>
int main()
{
int sequence1[8] = {1, 3, 2, 4, 5, 7, 6, 9};
int sequence2[8] = {1, 3, 2, 4, 5, 7, 6, 9};
int sequence3[8] = {1, 3, 2, 4, 5, 7, 6};
bool lhsIsLessThanRhs = false;
lhsIsLessThanRhs = std::lexicographical_compare(&sequence1[0],
&sequence1[8],
&sequence2[0],
&sequence2[8]);
std::cout << lhsIsLessThanRhs << " "; // 0
lhsIsLessThanRhs = std::lexicographical_compare(&sequence2[0],
&sequence2[8],
&sequence3[0],
&sequence3[8]);
std::cout << lhsIsLessThanRhs << " "; // 0
lhsIsLessThanRhs = std::lexicographical_compare(&sequence3[0],
&sequence3[8],
&sequence1[0],
&sequence1[8]);
std::cout << lhsIsLessThanRhs << " "; // 1
std::cout << std::endl;
return 0;
}
// Output: 0 0 1
Reference: Josuttis, Nicolai M., The C++ Standard Library: A Tutorial and Reference. New York: Addison-Wesley, 1999, pp. 360-2.
Tuesday, December 19, 2017
C++98: std::lexicographical_compare()
std::lexicographical_compare() tests for ‘lexigraphically less than’ on two ranges. For strings, it is functionally equivalent to ‘<’. Here is an example:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment