multimap insert ( iterator position, const value_type& x )

Search:
<script type=text/javascript> </script>
login:
remember me[register]
--
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
multimap
comparison operators
multimap::multimap
multimap::~multimap
member functions:
· multimap::begin
· multimap::clear
· multimap::count
· multimap::empty
· multimap::end
· multimap::equal_range
· multimap::erase
· multimap::find
· multimap::get_allocator
· multimap::insert
· multimap::key_comp
· multimap::lower_bound
· multimap::max_size
· multimap::operator=
· multimap::rbegin
· multimap::rend
· multimap::size
· multimap::swap
· multimap::upper_bound
· multimap::value_comp

-

multimap::insertpublic member function
iterator insert ( const value_type& x );
iterator insert ( iterator position, const value_type& x );
template <class InputIterator>
   void insert ( InputIterator first, InputIterator last );

Insert element

The multimap container is extended by inserting a single new element (if parameter x is used) or a sequence of elements (if input iterators are used).

This effectively increases the container size by the amount of elements inserted.

Internally, multimap containers keep all their elements sorted following the criterion specified by its comparison object on construction. Each element is inserted in its respective position following this ordering.

Efficiency of this operation can be dramatically improved by providing the appropriate value in parameter position.

Parameters

x
Value to be used to initialize the inserted element.
value_type is a member type defined in multimap containers as pair<const Key,T>, where Key is the first template parameter (the key type) and T is the second template parameter (the mapped type).
position
Position of the first element to be compared for the insertion operation.
Notice that this does not force the new element to be in that position within the multimap container (elements in a multimap always follow a specific ordering), but this is actually an indication of a possible insertion position in the container that, if set to the element that precedes the actual location where the element is inserted, makes for a very efficient insertion operation.
iterator is a member type, defined as a bidirectional iterator type.
first, last
Iterators specifying a range of elements. Copies of the elements in the range [first,last) are inserted in the multimap container.
Notice that the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.
The template type can be any type of input iterator.

Return value

In the versions returning a value, this is an iterator pointing to the newly inserted element in the multimap.

iterator is a member type, defined as a bidirectional iterator type.
Dereferencing this iterator accesses the element's value, which is of type pair<const Key,T>.

Example

// multimap::insert
#include <iostream>
#include <map>
using namespace std;

int main ()
{
  multimap<char,int> mymultimap;
  multimap<char,int>::iterator it;

  // first insert function version (single parameter):
  mymultimap.insert ( pair<char,int>('a',100) );
  mymultimap.insert ( pair<char,int>('z',150) ); 
  it=mymultimap.insert ( pair<char,int>('b',75) );

  // second insert function version (with hint position):
  mymultimap.insert (it, pair<char,int>('c',300));  // max efficiency inserting
  mymultimap.insert (it, pair<char,int>('z',400));  // no max efficiency inserting

  // third insert function version (range insertion):
  multimap<char,int> anothermultimap;
  anothermultimap.insert(mymultimap.begin(),mymultimap.find('c'));

  // showing contents:
  cout << "mymultimap contains:/n";
  for ( it=mymultimap.begin() ; it != mymultimap.end(); it++ )
    cout << (*it).first << " => " << (*it).second << endl;

  cout << "anothermultimap contains:/n";
  for ( it=anothermultimap.begin() ; it != anothermultimap.end(); it++ )
    cout << (*it).first << " => " << (*it).second << endl;

  return 0;
}
Output:
element 'z' already existed with a value of 200
mymultimap contains:
a => 100
b => 75
c => 300
z => 150
z => 400
anothermap contains:
a => 100
b => 75

Complexity

For the first version ( insert(x) ), logarithmic.
For the second version ( insert(position,x) ), logarithmic in general, but amortized constant if x is inserted right after the element pointed by position.
For the third version ( insert (first,last) ), Nlog(size+N) in general (where N is the distance between first and last, and size the size of the container before the insertion), but linear if the elements between first and last are already sorted according to the same ordering criterion used by the container.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值