Solved

multimap sample

Posted on 2002-07-10
6
621 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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

820 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