Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

multimap sample

Posted on 2002-07-10
6
Medium Priority
?
638 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
Independent Software Vendors: 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 1600 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

670 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