efratk
asked on
multimap sample
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 :-)
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 :-)
/// 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;
}
#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;
}
// 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;
}
#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;
}
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ambience,
thank u very much
efrat
thank u very much
efrat
#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;
}