TheDeveloperBlog.com

Home | Contact Us

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

C++ set find() Function

C++ set find() Function 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++ set find()

C++ set find() function is used to find an element with the given value val. If it finds the element then it returns an iterator pointing to the element otherwise, it returns an iterator pointing to the end of the set i.e. set::end().

Syntax

         iterator find (const value_type& val) const;                  // until C++ 11

   const_iterator find (const value_type& val) const;              //since C++ 11
         iterator       find (const value_type& val);                    //since C++ 11

Parameter

val: specifies the value to be searched in the set container.

Return value

If it finds the element then it returns an iterator pointing to the element otherwise, it returns an iterator pointing to the end of the set i.e. set::end().

Complexity

Logarithmic in size.

Iterator validity

No changes.

Data Races

The container is accessed (neither the const nor the non-const versions modify the container.

No mapped values are accessed: concurrently accessing and modifying the elements is safe.

Exception Safety

If an exception is thrown, there are no changes in the container.

Example 1

Let's see the simple example to find the element with the given key value:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   set<int> m = {100,200,300,400};

   auto it = m.find(300);

   cout << "Iterator points to " << *it << endl;

   return 0;
}

Output:

Iterator points to 300

Example 2

Let's see a simple example to find the element:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   set<char> m = {'a', 'b', 'c', 'd'};

            
    auto it = m.find('e');
   
    if ( it == m.end() ) {
    // not found
     cout<<"Element not found";
    } 
    else {
        // found
        cout << "Iterator points to " << *it<< endl;
    }
    
   return 0;
}

Output:

Element not found

In the above example, find() function finds the key value e in the set m, if it is not found in the set then it will return a not found message otherwise, it will display the set.

Example 3

Let's see a simple example:

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

int main()
{
    char n;
    set<char> example = {'a','b','c','d','e'};
    
    cout<<"Enter the element which you want to search: ";
    cin>>n;
 
    auto search = example.find(n);
    if (search != example.end()) {
        cout << n<<" found and the value is " << *search << '\n';
    } else {
        cout << n<<" not found\n";
    }
}

Output:

Enter the element which you want to search: b
b found and the value is b

In the above example, find() function is used to find the element according to user's given value.

Example 4

Let's see a simple example:

#include <iostream>
#include <set>

int main () {
   std::set<int> myset;
   std::set<int>::iterator it;

   for (int i = 1; i <= 10; i++) myset.insert(i*10);    
   it = myset.find(40);
   myset.erase (it);
   myset.erase (myset.find(60));

   std::cout << "myset contains:";
   for (it = myset.begin(); it!=myset.end(); ++it)
      std::cout << ' ' << *it;
   std::cout << '\n';

   return 0;
}

Output:

myset contains: 10 20 30 50 70 80 90 100





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