A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/cpp_standard_library/cpp_vector_operator_greater_than.htm below:

C++ vector::operator>() Function

C++ vector::operator>() Function

The C++ vector::operator>() function is used to test whether first vector is greater than other or not, it returns true if the vector on the left side of the operator is greater than the vector that is on the right side 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.

Syntax

Following is the syntax for C++ vector::operator>() Function −

bool operator>(const vector<Type, Allocator>& left, const vector<Type, Allocator>& right);
Parameters Example 1

Let's consider the following example, where we are going to use the opertor>() function.

#include <iostream>
#include <vector>

int main(){
   std::vector<int> myvector1{11, 22, 33};
   std::vector<int> myvector2{74, 84, 94, 104};
   std::cout << std::boolalpha;
   std::cout << "myvector1 >  myvector2 is :  " << (myvector1 > myvector2) << '\n';
}
Output

When we compile and run the above program, this will produce the following result −

myvector1 >  myvector2 is :  false
Example 2

Considering the another scenario, where we are going to take char values and comparing them.

#include <iostream>
#include <vector>

int main(){
   std::vector <char> myvector1{'W','E','L','C','O','M','E'};
   std::vector <char> myvector2{'T','P'};
   std::cout << std::boolalpha;
   std::cout << "myvector1 >  myvector2 is :  " << (myvector1 > myvector2) << '\n';
}
Output

On running the above program, it will produce the following result −

myvector1 >  myvector2 is :  true
Example 3

In the following example, we are going to push_back() function to insert the values and applying operator>() function.

#include <iostream>
#include <vector>
using namespace std;

int main(void) {
   vector<int> v1 = {1, 2, 3, 4, 5};
   vector<int> v2;
   if (v1 > v2)
      cout << "v1 is greater than v2" << endl;
   v2.push_back(1);
   v2.push_back(2);
   v2.push_back(3);
   v2.push_back(4);
   v2.push_back(6);
   if (!(v1 > v2))
      cout << "v1 is not greater than v2" << endl;
   return 0;
}
Output

When we execute the above program, it will produce the following result −

v1 is greater than v2
v1 is not greater than v2

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