Standard Template Library (STL) learn to explore the Multimap container

Source: Internet
Author: User

C + + Multimaps and maps are very similar, but multimaps allow duplicate elements. (Refer to map container for specific usage)
List of functions:
Begin () returns the iterator that points to the first element
Clear () Delete all elements
COUNT () returns the number of occurrences of an element
Empty () returns true if Multimap is null
End () returns an iterator pointing to the end of the Multimap
Equal_range () returns an iterator that points to the element's key as the specified value
Erase () Delete element
Find () finds elements
Get_allocator () returns the Multimap Configurator
Insert () Inserts an element
Key_comp () returns a function that compares key
Lower_bound () returns the key value >= the first position of a given element
Max_size () returns the maximum number of elements that can be accommodated
Rbegin () returns a reverse iterator pointing to the tail of the Mulitmap
Rend () returns a reverse iterator pointing to the Multimap head
Size () returns the number of elements in the Multimap
Swap () swap two x multimaps
Upper_bound () returns the key value > the first position of a given element
Value_comp () returns a function that compares the element value

////////////////////////////////////////////////////////////////////////////////////
constructor function
Explicit Multimap (const pred& comp = Pred (), const a& AL = A ());
Multimap (const multimap& x);
Multimap (const value_type *first, const value_type *last,
Const pred& COMP = Pred (), const a& AL = A ());

Begin
Grammar:
Iterator begin ();
The Begin () function returns an iterator that points to the first element of the Multimap.
Clear
Grammar:
void Clear ();
The clear () function deletes all elements in the Multimap.
Count
Grammar:
Size_type count (const key_type &key);
The count () function returns the number of elements in the multimap that have the key value equal to key.
Empty
Grammar:
bool Empty ();
The empty () function returns True (TRUE) if Multimap is empty, otherwise false (false) is returned.
End
Grammar:
Iterator End ();
The end () function returns an iterator that points to the tail of the Multimap.
Equal_range
Grammar:
Pair Equal_range (const key_type &key);
The Equal_range () function finds all the elements in the multimap that have the key value equal to key, and returns two iterators that indicate a range.
Erase
Grammar:
void Erase (iterator pos);
void Erase (iterator start, iterator end);
Size_type Erase (const key_type &key);
The erase () function deletes the element at the POS position, or deletes the element between start and end, or removes all elements whose value is key.
Find
Grammar:
Iterator Find (const key_type &key);
The Find () function returns an iterator that points to an element with a key value, and returns an iterator to the tail of the multimap if it is not found.
Get_allocator
Grammar:
Allocator_type Get_allocator ();
The Get_allocator () function returns the Multimap configurator.
Insert
Grammar:
Iterator Insert (iterator pos, const TYPE &val);
void Insert (input_iterator start, Input_iterator end);
Pair insert (const TYPE &val);
Insert () function:

Insert Val to the back of the POS, and then return an iterator that points to the element.
Inserts the start-to-end element into the Multimap.
Insert Val only if Val does not exist. The return value is an iterator that points to the inserted element and a bool value that describes whether to insert it.
Key_comp
Grammar:
Key_compare Key_comp ();
The Key_comp () function returns a function that compares key.
Lower_bound
Grammar:
Iterator Lower_bound (const key_type &key);
The Lower_bound () function returns an iterator that points to the first element of the key value >=key in Multimap.
Max_size
Grammar:
Size_type max_size ();
The Max_size () function returns the maximum number of elements that Multimap can hold.
Rbegin
Grammar:
Reverse_iterator Rbegin ();
The Rbegin () function returns a reverse iterator that points to the tail of the Multimap.
Rend
Grammar:
Reverse_iterator rend ();
The rend () function returns a reverse iterator that points to the Multimap head.
Size
Grammar:
Size_type size ();
The size () function returns the number of elements saved in Multimap.
Swap
Grammar:
void swap (Multimap &obj);
Swap () swaps the elements in obj and now Mulitmap.
Upper_bound
Grammar:
Iterator Upper_bound (const key_type &key);
The Upper_bound () function returns an iterator that points to the first element of the key value >key in Multimap.
Value_comp
Grammar:
Value_compare Value_comp ();
The Value_comp () function returns a function that compares the element value.
Example:
#include <iostream>
#include <map>
#include <string>
using namespace Std;
void Main ()
{
Creation of Multimap <string,int> M;//multimap
M.insert (pair<string,int> ("Jack", 1));//Insert
M.insert (pair<string,int> ("Jack", 2));
M.insert (pair<string,int> ("Body", 1));
M.insert (pair<string,int> ("Navy", 4));
M.insert (pair<string,int> ("Demo", 3));

Multimap<string,int>::iterator ITER;
for (iter = M.begin (); ITER! = M.end (); ++iter)//traversal
{
cout<< (*iter) .first<< "" << (*iter) .second<<endl;
}
M.erase ("Navy"); deletion of//multimap
cout<< "The element after delete:" <<endl;
for (iter = M.begin (); ITER! = M.end (); ++iter)
{
cout<< (*iter) .first<< "" << (*iter) .second<<endl;
}
Multimap Element Lookup
Multimap<string,int>::iterator it;
int Num=m.count ("Jack");
it = M.find ("Jack");
cout<< "The search result is:" <<endl;
for (int i=1;i<=num;i++)
{
cout<< (*it) .first<< "" << (*it) .second<<endl;
it++;
}
if (i==1) {cout<< "Can not find!" <<endl; }
}
Output Result:
Body 1
Demo 3
Jack 1
Jack 2
Navy 4
The element after delete:
Body 1
Demo 3
Jack 1
Jack 2
The search result is:
Jack 1
Jack 2

Standard Template Library (STL) learn to explore the Multimap container

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.