STL_map——map::insert
来源:互联网 发布:风景线打印软件 编辑:程序博客网 时间:2024/06/11 10:18
Reference:
Inserts an element or a range of elements into a map.
Function:
pair <iterator, bool> insert(
const value_type& _Val
);
iterator insert(
iterator _Where,
const value_type& _Val
);
template<class InputIterator>
void insert(
InputIterator _First,
InputIterator _Last
_Val:The value of an element to be inserted into the map unless the map already contains that element or, more generally, an element whose key is equivalently ordered.
_Where:A hint regarding the place to start searching for the correct point of insertion.
_First:The position of the first element to be copied from a map.
_Last:The position just beyond the last element to be copied from a map.
Return Value:
The first insert member function returns a pair whose bool component returns true if an insertion was made and false if the map already contained an element whose key had an equivalent value in the ordering, and whose iterator component returns the address where a new element was inserted or where the element was already located.
To access the iterator component of a pair pr returned by this member function, use pr.first, and to dereference it, use *(pr.first). To access the bool component of a pair pr returned by this member function, use pr.second, and to dereference it, use *(pr.second).
The second insert member function, the hint version, returns an iterator that points to the position where the new element was inserted into the map.
Remarks:
The value_type of an element is a pair, so that the value of an element will be an ordered pair with the first component equal to the key value and the second component equal to the data value of the element.
Insertion can occur in amortized constant time for the hint version of insert, instead of logarithmic time, if the insertion point immediately follows _Where.
The third member function inserts the sequence of element values into a map corresponding to each element addressed by an iterator of in the range [_First, _Last) of a specified set.
Example:
Output:
The original key values of m1 = 1 2 3 4.
The original mapped values of m1 = 10 20 30 40.
The element 10 already exists in m1
with a key value of ( (pr.first) -> first ) = 1.
After the insertions, the key values of m1 = 1 2 3 4 5,
and the mapped values of m1 = 10 20 30 40 50.
After the insertions, the key values of m2 = 2 3 4 10,
and the mapped values of m2 = 20 30 40 100.
Inserts an element or a range of elements into a map.
Function:
pair <iterator, bool> insert(
const value_type& _Val
);
iterator insert(
iterator _Where,
const value_type& _Val
);
template<class InputIterator>
void insert(
InputIterator _First,
InputIterator _Last
);
Parameters:
_Val:The value of an element to be inserted into the map unless the map already contains that element or, more generally, an element whose key is equivalently ordered.
_Where:A hint regarding the place to start searching for the correct point of insertion.
_First:The position of the first element to be copied from a map.
_Last:The position just beyond the last element to be copied from a map.
Return Value:
The first insert member function returns a pair whose bool component returns true if an insertion was made and false if the map already contained an element whose key had an equivalent value in the ordering, and whose iterator component returns the address where a new element was inserted or where the element was already located.
To access the iterator component of a pair pr returned by this member function, use pr.first, and to dereference it, use *(pr.first). To access the bool component of a pair pr returned by this member function, use pr.second, and to dereference it, use *(pr.second).
The second insert member function, the hint version, returns an iterator that points to the position where the new element was inserted into the map.
Remarks:
The value_type of an element is a pair, so that the value of an element will be an ordered pair with the first component equal to the key value and the second component equal to the data value of the element.
Insertion can occur in amortized constant time for the hint version of insert, instead of logarithmic time, if the insertion point immediately follows _Where.
The third member function inserts the sequence of element values into a map corresponding to each element addressed by an iterator of in the range [_First, _Last) of a specified set.
Example:
#include <map>#include <iostream>int main( ){ using namespace std; map <int, int>::iterator m1_pIter, m2_pIter; map <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 ) ); m1.insert ( Int_Pair ( 4, 40 ) ); 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; pair< map<int,int>::iterator, bool > pr; pr = m1.insert ( Int_Pair ( 1, 10 ) ); if( pr.second == true ) { cout << "The element 10 was inserted in m1 successfully." << endl; } else { cout << "The element 10 already exists in m1\n" << "with a key value of ( (pr.first) -> first ) = " << ( pr.first ) -> first << "." << endl; } // The hint version of insert m1.insert( --m1.end( ), Int_Pair ( 5, 50 ) ); 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;}
Output:
The original key values of m1 = 1 2 3 4.
The original mapped values of m1 = 10 20 30 40.
The element 10 already exists in m1
with a key value of ( (pr.first) -> first ) = 1.
After the insertions, the key values of m1 = 1 2 3 4 5,
and the mapped values of m1 = 10 20 30 40 50.
After the insertions, the key values of m2 = 2 3 4 10,
and the mapped values of m2 = 20 30 40 100.
0 0
- STL_map——map::insert
- STL_map——map::begin
- STL_map——map::clear
- STL_map——map::count
- STL_map——map::empty
- STL_map——map::end
- STL_map——map::equal_range
- STL_map——map::erase
- STL_map——map::find
- STL_map——map::size
- STL_map
- STL_map
- STL_MAP
- STL_map
- STL_map
- map::insert
- map::insert and map::find
- map insert使用
- 20140720(找apt-get安装在哪)
- 自己制作Android包含@hide接口的SDK
- 索引器与数组类定义练习
- linux下epoll服务器
- iOS 在UILabel显示不同的字体和颜色
- STL_map——map::insert
- 如何清除jsp页面缓存、cookie、session
- 第六课 Struts的视图组件
- 2014-7-20
- codeforces356A Knight Tournament
- 2013成都邀请赛J题||HDU4725 The Shortest Path in Nya Graph(spfa+slf优化最短路)
- STL_map——map::size
- linux下android连接真机测试
- BFS与DFS总结