The C++ vector::operator<=() function is used to test whether first vector is less than or equal to other or not, it returns true if the left side of the operator is less than or equal to the vector that is on the right side of the vector or else it returns false. Operator <= compares element sequentially and comparison stops at first mismatch. This member function never throw an exception and the time complexity of the operator<=() function is linear.
SyntaxFollowing is the syntax for C++ vector::operator<=() Function −
bool operator<=(const vector<Type, Allocator>& left, const vector<Type, Allocator>& right);Parameters
Let's consider the following example, where we are going to use the opertor<=() function.
#include <iostream> #include <vector> using namespace std; int main (){ vector<int> myvector1 {123,234,345}; vector<int> myvector2 {123,234,345}; if (myvector1 <= myvector2) cout<<"myvector1 is less than or equal to myvector2.\n"; else cout<<"myvector1 is not less than or equal to myvector2.\n"; return 0; }Output
When we compile and run the above program, this will produce the following result −
myvector1 is less than or equal to myvector2.Example 2
Considering the another scenario, where we are going to take string values and comparing them.
#include <iostream> #include <vector> using namespace std; int main (){ vector<string> myvector1 {"abc","bcd","cde"}; vector<string> myvector2 {"abc","bcd"}; if (myvector1 <= myvector2) cout<<"myvector1 is less than or equal to myvector2.\n"; else cout<<"myvector1 is not less than or equal to myvector2.\n"; return 0; }Output
On running the above program, it will produce the following result −
myvector1 is not less than or equal to myvector2.Example 3
In the following example, we are going to push_back() function to insert the values and applying operator<=() function.
#include <vector> #include <iostream> int main( ){ using namespace std; vector <int> myvector1, myvector2; myvector1.push_back( 1 ); myvector1.push_back( 2 ); myvector1.push_back( 4 ); myvector2.push_back( 1 ); myvector2.push_back( 23 ); if ( myvector1 <= myvector2 ) cout << "myvector1 is less than or equal to vector myvector2." << endl; else cout << "myvector1 is greater than vector myvector2." << endl; }Output
When we execute the above program, it will produce the following result −
myvector1 is less than or equal to vector myvector2.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4