C++ Vector Library - operator<= Function


Advertisements

Description

The C++ function std::vector::operator<= tests whether first vector is less than or equal to other or not.

Operator <= compares element sequentially and comparison stops at first mismatch.

Declaration

Following is the declaration for std::vector::operator<= function form std::vector header.

template <class T, class Alloc>
bool operator<= (const vector<T,Alloc>& v1, const vector<T,Alloc>& v2);
  • v1 − First vector.

  • v2 − Second vector.

Return value

Returns true if first vector is less than or equal to second otherwise false.

Exceptions

This function never throws exception.

Time complexity

Linear i.e. O(n)

Example

The following example shows the usage of std::vector::operator<= function.

#include <iostream>
#include <vector>

using namespace std;

int main(void) {
   vector<int> v1 = {1, 2};
   vector<int> v2 = {1, 2, 3, 4, 5};

   if (v1 <= v2)
      cout << "1. v1 is less than or equal to v2" << endl;

   v1 = v2;

   if (v1 <= v2)
      cout << "2. v1 is less than or equal to v2" << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

1. v1 is less than or equal to v2
2. v1 is less than or equal to v2
vector.htm
Advertisements