TheDeveloperBlog.com

Home | Contact Us

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

C++ set lower_bound() Function

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

C++ set lower_bound() function is used to return an iterator pointing to the key in the set container which is equivalent to val passed in the parameter.

If val is not present in the set container, it returns an iterator pointing to the immediate next element which is just greater than val.

Syntax

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

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

Parameter

val: Value to be searched in the set container.

Return value

It returns an iterator pointing to the value in the set container which is equivalent to val passed in the parameter. If there is no such element return 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).

Concurrently accessing the elements of a set 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 get the lower bound of given key:

#include <iostream>
#include <set>

using namespace std;

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

   cout << "Lower bound is(=) " << *it;
   return 0;
}

Output:

Lower bound is(=) c

In the above example, lower bound of c is c.

Example 2

Let's see a simple example to erase the element of set from lower bound to upper bound:

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  set<int> myset;
  set<int>::iterator itlow,itup;

  for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90

  itlow=myset.lower_bound (30);                //       ^
  itup=myset.upper_bound (60);                 //       ^

  myset.erase(itlow,itup);                     // 10 20 70 80 90

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

  return 0;
}

Output:

myset contains: 10 20 70 80 90

In the above example, erase() function erased the element of set from lower bound(=) to upper bound(>) and print the remaining content.

Example 3

Let's see a simple example:

#include <set>  
#include <iostream>  

using namespace std;
  
int main( )  
{  
   using namespace std;  
   set <int> s1;  
   set <int> :: const_iterator s1_AcIter, s1_RcIter;  
  
   s1.insert( 10 );  
   s1.insert( 20 );  
   s1.insert( 30 );  
  
   s1_RcIter = s1.lower_bound( 20 );  
   cout << "The element of set s1 with a key of 20 is: "  
        << *s1_RcIter << "." << endl;  
  
   s1_RcIter = s1.lower_bound( 40 );  
  
   // If no match is found for the key, end( ) is returned  
   if ( s1_RcIter == s1.end( ) )  
      cout << "The set s1 doesn't have an element "  
           << "with a key of 40." << endl;  
   else  
      cout << "The element of set s1 with a key of 40 is: "  
           << *s1_RcIter << "." << endl;  
  
   // The element at a specific location in the set can be found   
   // by using a dereferenced iterator that addresses the location  
   s1_AcIter = s1.end( );  
   s1_AcIter--;  
   s1_RcIter = s1.lower_bound( *s1_AcIter );  
   cout << "The element of s1 with a key matching "  
        << "that of the last element is: "  
        << *s1_RcIter << "." << endl;  
        
        return 0;
}  

Output:

The element of set s1 with a key of 20 is: 20.
The set s1 doesn't have an element with a key of 40.
The element of s1 with a key matching that of the last element is: 30.

Example 4

Let's see a simple example:

#include<set>
#include<iostream>

using namespace std;
 
int main()
{
 
    set<int> mp; 
    // insert elements in random order
    mp.insert( 2 );
    mp.insert( 1 );
    mp.insert( 5 );
    mp.insert( 4 );
    
    cout<<"Elements are: \n";
    for (auto it = mp.begin(); it != mp.end(); it++) {
        cout << (*it)<< endl;
    }
 
    // when 2 is present
    auto it = mp.lower_bound(2);
    cout << "The lower bound of key 2 is ";
    cout << (*it)<< endl;
 
    // when 3 is not present
    // points to next greater after 3
    it = mp.lower_bound(3);
    cout << "The lower bound of key 3 is ";
    cout << (*it)<< endl;
 
    // when 6 exceeds
    it = mp.lower_bound(6);
    cout << "The lower bound of key 6 is ";
    cout << (*it);
    
    return 0;
}

Output:

Elements are: 
1
2
4
5
The lower bound of key 2 is 2
The lower bound of key 3 is 4
The lower bound of key 6 is 4

In the above example, when we try to find the lower bound of a value which exceeds the container or we can say that which is not present in the set container then it will return to the end .






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