C++ <list> - operator< Function
The C++ <list> operator< function is used to check whether the first list is less than the second list or not. It returns true if the first list is less than the second list, else returns false. operator< compares elements of lists sequentially and stops comparison after first mismatch.
Syntax
template <class T, class Alloc> bool operator< (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs);
template <class T, class Alloc> bool operator< (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs);
Parameters
lhs |
First list. |
rhs |
Second list. |
Return Value
Returns true if the contents of lhs are lexicographically less than the contents of rhs, else returns false.
Time Complexity
Linear i.e, Θ(n).
Example:
In the example below, the operator< function is used to check whether the first list is less than the second list or not.
#include <iostream> #include <list> using namespace std; int main (){ list<int> list1 {10, 20, 30}; list<int> list2 {10, 20, 30}; list<int> list3 {40, 50, 60}; if (list1 < list2) cout<<"list1 is less than list2.\n"; else cout<<"list1 is not less than list2.\n"; if (list1 < list3) cout<<"list1 is less than list3.\n"; else cout<<"list1 is not less than list3.\n"; return 0; }
The output of the above code will be:
list1 is not less than list2. list1 is less than list3.
❮ C++ <list> Library