Solved

multimap sample

Posted on 2002-07-10
6
629 Views
Last Modified: 2011-09-20
hi,
i need a simple sample that show basic usage of stl multimap.
insert, delete, move through members of specific key, etc.

thank u in advance
efratk :-)
0
Comment
Question by:efratk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 22

Expert Comment

by:ambience
ID: 7145366
//////// Demonstrates delete and insert

#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   multimap <int, int> m1, m2, m3;
   multimap <int, int> :: iterator pIter, Iter1, Iter2;
   int i, n;
   typedef pair <int, int> Int_Pair;

   for ( i = 1 ; i < 5 ; i++ )
   {
      m1.insert ( Int_Pair ( i, i )  );
      m2.insert ( Int_Pair ( i, i*i )  );
      m3.insert ( Int_Pair ( i, i-1 )  );
   }

   // The 1st member function removes an element at a given position
   Iter1 = ++m1.begin( );
   m1.erase( Iter1 );

   cout << "After the 2nd element is deleted, "
        << "the multimap ms1 is:" ;
   for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
      cout << " " << pIter -> second;
   cout << "." << endl;

   // The 2nd member function removes elements
   // in the range [_First, _Last)
   Iter1 = ++m2.begin( );
   Iter2 = --m2.end( );
   m2.erase( Iter1, Iter2 );

   cout << "After the middle two elements are deleted, "
        << "the multimap m2 is:" ;
   for ( pIter = m2.begin( ) ; pIter != m2.end( ) ; pIter++ )
      cout << " " << pIter -> second;
   cout << "." << endl;

   // The 3rd member function removes elements with a given _Key
   m3.insert( Int_Pair ( 2, 5 ) );
   n = m3.erase( 2 );

   cout << "After the element with a key of 2 is deleted,\n"
        << "the multimap m3 is:" ;
   for ( pIter = m3.begin( ) ; pIter != m3.end( ) ; pIter++ )
      cout << " " << pIter -> second;
   cout << "." << endl;

   // The 3rd member function returns the number of elements removed
   cout << "The number of elements removed from m3 is: "
        << n << "." << endl;

   // The dereferenced iterator can also be used to specify a key
   Iter1 = ++m3.begin( );
   m3.erase( Iter1 );

   cout << "After another element with a key equal to that"
        << endl;
   cout  << "of the 2nd element is deleted, "
         << "the multimap m3 is:" ;
   for ( pIter = m3.begin( ) ; pIter != m3.end( ) ; pIter++ )
      cout << " " << pIter -> second;
   cout << "." << endl;
}
0
 
LVL 22

Expert Comment

by:ambience
ID: 7145369
/// Demostrates find

#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   multimap <int, int> m1;
   multimap <int, int> :: const_iterator m1_AcIter, m1_RcIter;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );
   m1.insert ( Int_Pair ( 3, 20 ) );
   m1.insert ( Int_Pair ( 3, 30 ) );

   m1_RcIter = m1.find( 2 );
   cout << "The element of multimap m1 with a key of 2 is: "
        << m1_RcIter -> second << "." << endl;

   m1_RcIter = m1.find( 3 );
   cout << "The first element of multimap m1 with a key of 3 is: "
        << m1_RcIter -> second << "." << endl;

   // If no match is found for the key, end( ) is returned
   m1_RcIter = m1.find( 4 );

   if ( m1_RcIter == m1.end( ) )
      cout << "The multimap m1 doesn't have an element "
           << "with a key of 4." << endl;
   else
      cout << "The element of multimap m1 with a key of 4 is: "
           << m1_RcIter -> second << "." << endl;

   // The element at a specific location in the multimap can be
   // found using a dereferenced iterator addressing the location
   m1_AcIter = m1.end( );
   m1_AcIter--;
   m1_RcIter = m1.find( m1_AcIter -> first );
   cout << "The first element of m1 with a key matching"
        << endl << "that of the last element is: "
        << m1_RcIter -> second << "." << endl;

   // Note that the first element with a key equal to
   // the key of the last element is not the last element
   if ( m1_RcIter == --m1.end( ) )
      cout << "This is the last element of multimap m1."
           << endl;
   else
      cout << "This is not the last element of multimap m1."
           << endl;
}
0
 
LVL 22

Expert Comment

by:ambience
ID: 7145377
// demostrates insert and traversals

#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   multimap <int, int>::iterator m1_pIter, m2_pIter;

   multimap <int, int> m1, m2;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );
   m1.insert ( Int_Pair ( 3, 30 ) );

   cout << "The original key values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> first;
   cout << "." << endl;

   cout << "The original mapped values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> second;
   cout << "." << endl;

   m1.insert ( Int_Pair ( 1, 10 ) );

   // The hint version of insert
   m1.insert( --m1.end( ), Int_Pair ( 4, 40 )  );

   cout << "After the insertions, the key values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> first;
   cout << "," << endl;

   cout << " and the mapped values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> second;
   cout << "." << endl;

   m2.insert ( Int_Pair ( 10, 100 ) );

   // The templatized version inserting a range
   m2.insert( ++m1.begin( ), --m1.end( ) );

   cout << "After the insertions, the key values of m2 =";
   for ( m2_pIter = m2.begin( ); m2_pIter != m2.end( ); m2_pIter++ )
      cout << " " << m2_pIter -> first;
   cout << "," << endl;

   cout << " and the mapped values of m2 =";
   for ( m2_pIter = m2.begin( ); m2_pIter != m2.end( ); m2_pIter++ )
      cout << " " << m2_pIter -> second;
   cout << "." << endl;
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:efratk
ID: 7145790
hi ambience,

first, i want to thank u a lot for the detailed answer.
but, i would like to know also how to move through all the members with a specific key.

thanks again
efratk
0
 
LVL 22

Accepted Solution

by:
ambience earned 400 total points
ID: 7145835
int main( )
{
   typedef multimap <int, int, less<int> > IntMMap;
   typedef IntMMap::iterator IntMMapIter;
   typedef pair <int, int> Int_Pair;

   IntMMap m1;
   IntMMapIter m1_RcIter;

   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );
   m1.insert ( Int_Pair ( 2, 30 ) );
   m1.insert ( Int_Pair ( 3, 30 ) );
   m1.insert ( Int_Pair ( 2, 40 ) );
   m1.insert ( Int_Pair ( 2, 50 ) );

   pair <IntMMapIter, IntMMapIter> p1, p2;
   p1 = m1.equal_range( 2 );

   IntMMapIter i = p1.first;
   for(; i != p1.second; ++i)
   {
        cout << "key of 2 has value: " << i-> second << "." << endl;

   }
   return 0;
}
0
 

Author Comment

by:efratk
ID: 7145873
ambience,
thank u very much
efrat
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question