Find element in multiset by value - C++ STL

C++ examples for STL:multiset

Description

Find element in multiset by value

Demo Code

#include <iostream> 
 #include <set> // multiset class-template definition 
 #include <algorithm> // copy algorithm 
 #include <iterator> // ostream_iterator 
using namespace std; 

// define short name for multiset type used in this program 
typedef multiset< int, less< int > > Ims; 

int main() //from   ww w . j  a  va 2  s .  c o m
{ 
   const int SIZE = 10; 
   int a[ SIZE ] = { 7, 22, 9, 1, 18, 30, 100, 22, 85, 13 }; 
   Ims intMultiset; // Ims is typedef for "integer multiset" 
   ostream_iterator< int > output( cout, " " ); 

   intMultiset.insert( 15 ); // insert 15 in intMultiset 
   intMultiset.insert( 15 ); // insert 15 in intMultiset 

   // iterator that cannot be used to change element values 
   Ims::const_iterator result; 

   // find 15 in intMultiset; find returns iterator 
   result = intMultiset.find( 15 ); 

   if ( result != intMultiset.end() ) // if iterator not at end 
       cout << "Found value 15\n"; // found search value 15 

   // find 20 in intMultiset; find returns iterator 
   result = intMultiset.find( 20 ); 

   if ( result == intMultiset.end() ) // will be true hence 
       cout << "Did not find value 20\n" ; // did not find 20 


}

Result


Related Tutorials