operator< (<string>)
Visual Studio 2012
Tests if the string object on the left side of the operator is less than to the string object on the right side.
template<class CharType, class Traits, class Allocator>
bool operator<(
const basic_string<CharType, Traits, Allocator>& _Left,
const basic_string<CharType, Traits, Allocator>& _Right
);
template<class CharType, class Traits, class Allocator>
bool operator<(
const basic_string<CharType, Traits, Allocator>& _Left,
const CharType *_Right
);
template<class CharType, class Traits, class Allocator>
bool operator<(
const CharType *_Left,
const basic_string<CharType, Traits, Allocator>& _Right
);
A lexicographical comparison between strings compares them character by character until:
-
It finds two corresponding characters unequal, and the result of their comparison is taken as the result of the comparison between the strings.
-
It finds no inequalities, but one string has more characters than the other, and the shorter string is considered less than the longer string.
-
It finds no inequalities and finds that the strings have the same number of characters, and so the strings are equal.
// string_op_lt.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
{
using namespace std;
// Declaring an objects of type basic_string<char>
string s1 ( "strict" );
string s2 ( "strum" );
cout << "The basic_string s1 = " << s1 << "." << endl;
cout << "The basic_string s2 = " << s2 << "." << endl;
// Declaring a C-style string
char *s3 = "strict";
cout << "The C-style string s3 = " << s3 << "." << endl;
// First member function: comparison between left-side object
// of type basic_string & right-side object of type basic_string
if ( s1 < s2 )
cout << "The string s1 is less than the string s2." << endl;
else
cout << "The string s1 is not less than the string s2." << endl;
// Second member function: comparison between left-hand object
// of type basic_string & right-hand object of C-syle string type
if ( s1 < s3 )
cout << "The string s1 is less than the string s3." << endl;
else
cout << "The string s1 is not less than the string s3." << endl;
// Third member function: comparison between left-hand object
// of C-syle string type & right-hand object of type basic_string
if ( s3 < s2 )
cout << "The string s3 is less than the string s2." << endl;
else
cout << "The string s3 is not less than the string s2." << endl;
}
The basic_string s1 = strict. The basic_string s2 = strum. The C-style string s3 = strict. The string s1 is less than the string s2. The string s1 is not less than the string s3. The string s3 is less than the string s2.