TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ multiset non member operator less than=

C++ multiset non member operator less than= with Examples on tutorial for beginners and professionals with examples on multiset, begin(), end(), multiset constructor, ~multiset destructor, operator=, rbegin(), rend() etc.

<< Back to CPP

C++ std operator<=

C++ Multiset Operator<= is a non-member overloaded function of multiset in C++. This function is used to check whether the first multiset is less than or equal to other or not.

Note: Operator <= compares element sequentially and at first mismatch comparison will stop.

Syntax

template <class T, class Compare, class Allocator>
bool operator<= ( const multiset<T,Compare,Allocator>& lhs,
                    const multiset<T,Compare,Allocator>& rhs );

Parameter

lhs: First multiset object.

rhs: Second multiset object.

Return value

It returns true if the left side of the multiset object is less than or equal to the right side of the multiset object otherwise false.

Complexity

Complexity will be constant, if the size of lhs and rhs is different.

Otherwise, up to linear in the size of lhs and rhs.

Iterator validity

No changes.

Data Races

Containers, lhs and rhs are accessed.

Concurrently accessing the elements of unmodified multiset is always safe.

Exception Safety

This function does not throw an exception.

Example 1

Let's see the simple example to check whether the first multiset is less than or equal to or not:

#include <iostream>
#include <set>

using namespace std;

int main() {
   multiset<char> m1;
   multiset<char> m2;

   m1.emplace('a');
   m2.emplace('a');

   if (m1 <= m2)
      cout << "Multiset m1 is less than or equal to m2." << endl;

   m1.emplace('b');

   if (!(m1 <= m2))
      cout << "Multiset m1 is not less than or equal to m2." << endl;

   return 0;
}

Output:

Multiset m1 is less than or equal to m2.
Multiset m1 is not less than or equal to m2.

In the above example, there are two multisets m1 and m2. m1 and m2 contains one element. When we compare both multisets, then it will display the message "multiset m1 is less than or equal to m2" and after adding one extra element in m1 then it will display the message that "multiset m1 is not less than or equal to m2".

Example 2

Let's see a simple example:

#include <set>  
#include <iostream>  
  
int main ()  
{  
   using namespace std;  
   multiset <int> m1, m2, m3, m4;  
   int i;  
  
   for (i = 1; i <3; i ++)  
   {  
      m1.insert (i);  
      m2.insert (i * i);  
      m3.insert ( i - 1);  
      m4.insert (i);  
   }  
  
   if (m1 <= m2)  
      cout << "The multiset m1 is less than or equal to the multiset m2." << endl;  
   else  
      cout << "The multiset m1 is greater than the multiset m2." << endl;  
  
   if (m1 <= m3)  
      cout << "The multiset m1 is less than or equal to the multiset m3." << endl;  
   else  
      cout << "The multiset m1 is greater than the multiset m3." << endl;  
  
   if (m1 <= m4)  
      cout << "The multiset m1 is less than or equal to the multiset m4." << endl;  
   else  
      cout << "The multiset m1 is greater than the multiset m4." << endl;  
      
      return 0;
}  

Output:

The multiset m1 is less than or equal to the multiset m2.
The multiset m1 is greater than the multiset m3.
The multiset m1 is less than or equal to the multiset m4.

Example 3

Let's see a simple example:

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

int  main () 
{ 
  multiset < int >  s1 ,  s2 ; 
  s1 . insert ( 10 ); 
  s1 . insert ( 20 ); 
  s1 . insert ( 30 ); 
  s2  =  s1 ;

  cout  <<  ( s1  <=  s2 )  << endl ;

  s1 . insert ( 40 );

  cout  <<  ( s1  <=  s2 )  << endl ; 
  
  return 0;
}

Output:

1
0

In the above example, if multiset s1 is less than or equal to s2 then it will return 1 otherwise 0.

Example 4

#include <set>  
#include <iostream>  

using namespace std; 
  
int main ()  
{  
   multiset<string> m2;
   typedef multiset<string> login; 
   
   m2.insert("xyz@123") ; //stored password
   
   string password;
   login m1;
   
       cout<<"---------Login----------"<<endl<<endl;
       cout<<"Enter password: \n";
       cin>> password;       // Get value
       m1.insert(password);   // Put them in multiset

     cout<<"Password you have entered: \n";
     for (auto it = m1.begin(); it != m1.end(); it++) {
        cout << (*it)<< endl;
      }
      cout<<"Password stored in the system :\n";
     for (auto it = m2.begin(); it != m2.end(); it++) {
        cout << (*it)<< endl;
     }
  
   if (m2 <= m1)  
        cout << "\nWelcome to your Page..." << endl; 
   else  
        cout << "\nIncorrect Password..." << endl; 
      
      return 0;
}

Output:

1).
---------Login----------

Enter password: 
[email protected]
Password you have entered: 
[email protected]
Password stored in the system:
[email protected]

Welcome to your Page...

2).
---------Login----------

Enter password: 
[email protected]
Password you have entered: 
[email protected]
Password stored in the system:
[email protected]

Incorrect Password...

In the above example, there are two multisets m1 and m2. m1 contains stored password and second multiset m2 stores user's entered password. It checks whether the m2 is less than or equal to m2 or not. If password of m2 is less than or equal to m1 then login is successful otherwise login fails.

Next TopicC++ multiset




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf