Solved

multimap sample

Posted on 2002-07-10
6
610 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
Comment Utility
//////// 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
Comment Utility
/// 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
Comment Utility
// 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:efratk
Comment Utility
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
Comment Utility
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
Comment Utility
ambience,
thank u very much
efrat
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 clear a vector as well as how to detect empty vectors in C++.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now