TheDeveloperBlog.com

Home | Contact Us

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

C++ set operator!=

C++ set operator!= with tutorial for beginners and professionals with examples on constructor, destructor, operator=(), begin(), cbegin(), cend(), end(), crbegin(), empty(), max_size(), clear(), emplace_hint(), key_comp(), swap() etc.

<< Back to CPP

C++ std operator!=

C++ std operator!= is a non-member overloaded function of set. This function is used to check whether the two sets are equal or not.

Note: Comparison between set objects is based on a pair wise comparison of the elements. Two sets are equal if they have the same number of elements and their corresponding elements have the same values. Otherwise they are not equal.

Syntax

template <class T, class Compare, class Alloc>
  bool operator!= ( const set<T,Compare,Alloc>& lhs,
                    const set<T,Compare,Alloc>& rhs );

Parameter

lhs: First set object.

rhs: Second set object.

Return value

It returns true if the left side of the set object is equal to the right side of the set 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 set is always safe.

Exception Safety

This function does not throw an exception.

Example 1

Let's see the simple example to check whether the two sets are equal or not:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   set<char> m1;
   set<char> m2;

   m1.emplace('a');

   if (m1 != m2)
      cout << "Both sets are not equal." << endl;

   m1 = m2;

   if (!(m1 != m2))
      cout << "Both sets are equal." << endl;

   return 0;
}

Output:

Both sets are not equal.
Both sets are equal.

In the above example, there are two sets m1 and m2. m1 contains one element and m2 is empty. When we compare both sets then it will display the message "both sets are not equal" and after assigning m1 to m2 both sets have equal element then it will display the message that "both sets are equal".

Example 2

Let's see a simple example:

#include <set>  
#include <iostream>  
  
int main ()  
{  
   using namespace std;  
   set <int> m1, m2, m3;  
   int i;  
   
   for (i = 0; i <3; i ++)  
   {  
      m1.insert (i);  
      m2.insert (i * i);  
      m3.insert (i);  
   }  
  
   if (m1!= m2)  
      cout << "The sets m1 and m2 are not equal." << endl;  
   else  
      cout << "The sets m1 and m2 are equal." << endl;  
  
   if (m1!= m3)  
      cout << "The sets m1 and m3 are not equal." << endl;  
   else  
      cout << "The sets m1 and m3 are equal." << endl;  
      
      return 0;
}

Output:

The sets m1 and m2 are not equal.
The sets m1 and m3 are equal.

Example 3

Let's see a simple example:

#include <iostream>
 #include <set>

using namespace std;

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

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

  s2 . insert ( 40 );

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

Output:

0
1

In the above example if set s1 and s2 are equal then it will return 1 otherwise 0.

Example 4

#include <set>  
#include <iostream>  
using namespace std; 
  
int main ()  
{  
   set<string> m2;
   typedef set<string> login; 
   
  m2 = {"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 set

     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 (m1 != m2)  
      cout << "\nIncorrect Password..." << endl;  
   else  
      cout << "\nWelcome to your Page..." << endl; 
      
      return 0;
}

Output:

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

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

Incorrect Password...

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

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

Welcome to your Page...

In the above example, there are two sets m1 and m2. m1 contains stored password and second set m2 stores user's entered password. It checks whether the both set has same elements or not. If password will match then login is successful otherwise login fails.


Next TopicSet operator<




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