C++ Programming Code Examples
C++ > Visual C++ 5.0 Standard C++ Library Code Examples
Algorithm lexicographical compare - Lexicographically compare two sequences.
Algorithm lexicographical compare - Lexicographically compare two sequences.
lexicographical_compare
Header
<algorithm>
template<class InputIterator1, InputIterator2>
bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2)
Lexicographically compare two sequences. The lexicographical comparison is a dictionary comparison in which words are compared for ordering. Compares two sequences [first1, last1) and [first2, last2) as follows: traverses the sequences, comparing corresponding pairs of elements e1 and e2: if e1 < e2, then return true. If e2 < e1, then return false, otherwise continue to the next corresponding pair of elements. If the first sequence gets exhausted but the second is not, return true, otherwise return false.
The non-predicate version uses operator== for comparison.
template<class InputIterator1, InputIterator2, class BinaryPredicate>
bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, BinaryPredicate pr)
Lexicographically compare two sequences. The lexicographical comparison is a dictionary comparison in which words are compared for ordering. Compares two sequences [first1, last1) and [first2, last2) as follows: traverses the sequences, comparing corresponding pairs of elements e1 and e2: if e1 < e2, then return true. If e2 < e1, then return false, otherwise continue to the next corresponding pair of elements. If the first sequence gets exhausted but the second is not, return true, otherwise return false.
The predicate version uses the predicate function pr for comparison.
Samples
#pragma warning (disable:4786)
#include <algorithm>
#include <iostream>
#include <string>
#include <cstring>
#include <functional>
int main()
{
std::string s1("Shaun"), s2("Shawn") ;
char *s3 = "Sammy" ;
char *s4 = "Sam" ;
int s3Len = strlen(s3) ;
int s4Len = strlen(s4) ;
bool result ;
//non-predicate version of lexicographical_compare
result = std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end()) ;
std::cout << "s1 = " << s1 << std::endl ;
std::cout << "s2 = " << s1 << std::endl ;
std::cout << "using non-predicate version of lexicographical_compare" << std::endl ;
if (result)
std::cout << "s1 < s2" << std::endl ;
else
std::cout << "s1 >= s2" << std::endl ;
//predicate version of lexicographical_compare
result = std::lexicographical_compare(s3, s3+s3Len, s4, s4+s4Len, std::less<char>()) ;
std::cout << "\n\ns3 = " << s3 << std::endl ;
std::cout << "s4 = " << s4 << std::endl ;
std::cout << "using non-predicate version of lexicographical_compare" << std::endl ;
if (result)
std::cout << "s3 < s4" << std::endl ;
else
std::cout << "s3 >= s4" << std::endl ;
return 0 ;
}
Program Output
s1 = Shaun
s2 = Shaun
using non-predicate version of lexicographical_compare
s1 < s2
s3 = Sammy
s4 = Sam
using non-predicate version of lexicographical_compare
s3 >= s4
Lexicographical less-than comparison. Returns true if the range [first1,last1) compares lexicographically less than the range [first2,last2). The C++ function std::algorithm::lexicographical_compare() tests whether one range is lexicographically less than another or not. A lexicographical comparison is the kind of comparison generally used to sort words alphabetically in dictionaries. A lexicographical comparison is the kind of comparison generally used to sort words alphabetically in dictionaries; It involves comparing sequentially the elements that have the same position in both ranges against each other until one element is not equivalent to the other. The result of comparing these first non-matching elements is the result of the lexicographical comparison. If both sequences compare equal until one of them ends, the shorter sequence is lexicographically less than the longer one.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.
Function object class for less-than inequality comparison. Binary function object class whose call returns whether the its first argument compares less than the second (as returned by operator <). Generically, function objects are instances of a class with member function operator() defined. This member function allows the object to be used with the same syntax as a function call. This function does not return any value.
Iterators are just like pointers used to access the container elements. Iterators are one of the four pillars of the Standard Template Library or STL in C++. An iterator is used to point to the memory address of the STL container classes. For better understanding, you can relate them with a pointer, to some extent. Iterators act as a bridge that connects algorithms to STL containers and allows the modifications of the data present inside the container. They allow you to iterate over the container, access and assign the values, and run different operators over them, to get the desired result. • Iterators are used to traverse from one element to another element, a process is known as iterating through the container. • The main advantage of an iterator is to provide a common interface for all the containers type. • Iterators make the algorithm independent of the type of the container used.
Templates are powerful features of C++ which allows us to write generic programs. Similar to function templates, we can use class templates to create a single class to work with different data types. Class templates come in handy as they can make our code shorter and more manageable. A class template starts with the keyword template followed by template parameter(s) inside <> which is followed by the class declaration. T is the template argument which is a placeholder for the data type used, and class is a keyword. Inside the class body, a member variable var and a member function functionName() are both of type T.
A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.
Get string length. Returns the length of the C string str. C++ strlen() is an inbuilt function that is used to calculate the length of the string. It is a beneficial method to find the length of the string. The strlen() function is defined under the string.h header file. The strlen() takes a null-terminated byte string str as its argument and returns its length. The length does not include a null character. If there is no null character in the string, the behavior of the function is undefined.
A C++ template is a powerful feature added to C++. It allows you to define the generic classes and generic functions and thus provides support for generic programming. Generic programming is a technique where generic types are used as parameters in algorithms so that they can work for a variety of data types. We can define a template for a function. For example, if we have an add() function, we can create versions of the add function for adding the int, float or double type values. Where Ttype: It is a placeholder name for a data type used by the function. It is used within the function definition. It is only a placeholder that the compiler will automatically replace this placeholder with the actual data type. class: A class keyword is used to specify a generic type in a template declaration.
Return iterator to beginning. Returns an iterator pointing to the first character of the string. This function gives a reference to the first element. The C++ string::begin function returns the iterator pointing to the first character of the string. Note that, Unlike the string::front function, which returns a direct reference to the first character, it returns the iterator pointing to the same character of the string. This function does not return any value.
In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.
Return iterator to end. Returns an iterator pointing to the past-the-end character of the string. The past-the-end character is a theoretical character that would follow the last character in the string. It shall not be dereferenced. Because the ranges used by functions of the standard library do not include the element pointed by their closing iterator, this function is often used in combination with string::begin to specify a range including all the characters in the string. If the object is an empty string, this function returns the same as string::begin.
A 'stack' is a basic "data structure" and can be defined in an 'Abstract', implementation-free manner, or it can be "Generally Defined" as a Linear List of items in which all additions and
In C++ code, 'Declare and define' the function test(). Within the try block check whether the value is 'greater than zero' or not. If the value greater than zero 'throw the value' and catch
To add two numbers using pointer in the C++, you have to enter the 2 number, then make 2 'pointer' type variable of same type say *ptr1 and *ptr2 to initialize the address of both the
In graph theory, an "edge coloring" of a graph is an assignment of colors to the edges of the graph so that no two adjacent edges have the same color. Any "2 edges" connected to same