Test two sequences for pairwise equality, Lexical comparison, Chapter 13 – HP Integrity NonStop H-Series User Manual
Page 171: For a
// example 1, a simple inner product
int in1 = inner_product(a, a+3, b, 0);
cout << "Inner product is " << in1 << endl;
// example 2, user defined operations
bool anyequal = inner_product(a, a+3, b, true,
logical_or
cout << "any equal? " << anyequal << endl;
}
Test Two Sequences for Pairwise Equality
The equal() algorithm tests two sequences for pairwise equality. By using an alternative binary predicate, it can also be used
for a wide variety of other pair-wise tests of parallel sequences. The arguments are simple input iterators:
bool equal (InputIterator first, InputIterator last,
InputIterator first2 [, BinaryPredicate] );
Equal and Mismatch
The equal() algorithm assumes, but does not verify, that the second sequence contains at least as many elements as the first.
A true result is generated if all values test equal to their corresponding element. The alternative version of the algorithm
substitutes an arbitrary boolean function for the equality test, and returns true if all pair-wise elements satisfy the predicate.
In the sample program this is illustrated by replacing the predicate with the greater_equal() function, and in this fashion true
will be returned only if all values in the first sequence are greater than or equal to their corresponding value in the second
sequence.
void equal_example ()
// illustrate the use of the equal algorithm
{
int a[] = {4, 5, 3};
int b[] = {4, 3, 3};
int c[] = {4, 5, 3};
cout << "a = b is: " << equal(a, a+3, b) << endl;
cout << "a = c is: " << equal(a, a+3, c) << endl;
cout << "a pair-wise greater-equal b is: "
<< equal(a, a+3, b, greater_equal
}
Lexical Comparison
A lexical comparison of two sequences can be described by noting the features of the most common example, namely the
comparision of two words for the purposes of placing them in "dictionary order." When comparing two words, the elements
(that is, the characters) of the two sequences are compared in a pair-wise fashion. As long as they match, the algorithm
advances to the next character. If two corresponding characters fail to match, the earlier character determines the smaller
word. So, for example, everybody is smaller than everything, since the b in the former word alphabetically precedes the t in
the latter word. Should one or the other sequence terminate before the other, than the terminated sequence is consider to be
smaller than the other. So, for example, every precedes both everybody and everything, but comes after eve. Finally, if both
sequences terminate at the same time, and, in all cases, pair-wise characters match, then the two words are considered to be
equal.
The lexicographical_compare() algorithm implements this idea, returning true if the first sequence is smaller than the
second, and false otherwise. The algorithm has been generalized to any sequence. Thus the lexicographical_comare()
algorithm can be used with arrays, strings, vectors, lists, or any of the other data structures used in the standard library.
bool lexicographical_compare
(InputIterator first1, InputIterator last1,