TheDeveloperBlog.com

Home | Contact Us

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

C++ multiset upper_bound() function

C++ multiset upper_bound() Function 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++ multiset upper_bound()

C++ Multiset upper_bound() function is used to return an iterator pointing to the value in the multiset container which is larger to val passed in the parameter.

Syntax

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

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

Parameter

val: value to be searched in the multiset container.

Return value

upper_bound() function returns an iterator pointing to the value in the multiset container which is larger 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 multiset container).

Concurrently accessing the elements of container is safe.

Exception

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

Example 1

Let's see the simple example to get the upper bound of given value:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   multiset<char> m = {'a', 'b', 'c', 'b'};
           
   auto it = m.upper_bound('b');

   cout << "Upper bound of b is(>): " << *it << endl;

   return 0;
}

Output:

Upper bound of b is(>): c

In the above example, when we try to find the upper bound of element b then it will return greater element of b i.e. c

Example 2

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

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  multiset<int> mymultiset;
  multiset<int>::iterator itlow,itup;

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

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

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

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

  return 0;
}

Output:

mymultiset contains: 10 20 70 80 90

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

Example 3

Let's see a simple example:

#include<iostream>
#include<set>
using namespace std;
 
int main()
{
    // initialize container
    multiset<int> mp;
 
    // insert elements in random order
    mp.insert( 12 );
    mp.insert( 11 );
    mp.insert( 15 );
    mp.insert( 14 );
 
    // when 11 is present
    auto it = mp.upper_bound(11);
    cout << "The upper bound of key 11 is ";
    cout << (*it)<< endl;
 
    // when 13 is not present
    it = mp.upper_bound(13);
    cout << "The upper bound of key 13 is ";
    cout << (*it)<< endl;
 
    // when 17 is exceeds the maximum key, so size
        // of mp is returned as key and value as 0.
    it = mp.upper_bound(17);
    cout << "The upper bound of key 17 is ";
    cout << (*it);
    return 0;
}

Output:

The upper bound of key 11 is 12
The upper bound of key 13 is 14
The upper bound of key 17 is 4

In the above example, when we try to find the upper bound of a value which is not present in the multiset container but does not exceed the maximum value then it will return greater value

i.e. when we trying to find upper bound of 13 then it will return 14 and when we trying to find upper bound of a value which is not present in the multiset and exceeds the maximum value of container then it will return to the end().

Example 4

Let's see a simple example:

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

Output:

The first element of multiset s1 with a key greater than 20 is: 30.
The multiset s1 doesn't have an element with a key greater than 30.
The first element of s1 with a key greater than
that of the initial element of s1 is: 20.
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