map

来源:互联网 发布:微信照片打印机软件 编辑:程序博客网 时间:2024/06/16 06:14
描述
基于键值用于存储/获取pair 的关联式容器,pair 包括两部分数据<key, data>,key 必须
唯一,底层使用RB-Tree 作为基础数据,因此数据在内部会基于键值进行自动排序,元素的
值可以直接修改,与key 不关联,而key 不可修改必须使用新<key,value>重新插入并删除原
来的<key,value>。
1. 关联式容器,大小可变,支持高效的基于key 的元素获取;
2. 可以反转,其迭代器为双向的;
3. 自排序;
4. 唯一(key);
5. 存储pair,含<key, data>;
6. 可根据需要定制函数对象从而调整map 排序元素;
使用
1. 定义
/*
* 描述: 定义模板类map
* 参数:
* @Key 键类型名称
* @Key 值类型名称
* @_Traits 函数对象类型,用来比较2 个元素之间的关系,map 依据此比较结果进
行排序
* @_Alloc 分配器
* 注意:
* @_Traits 以提供默认函数对象,使用小于比较作为排序依据
* @_Alloc 参数已提供默认分配器,多数情况下使用默认分配器可满足应用
*/
template <class Key, class Type, class _Traits = less<Key>,
class _Alloc=allocator<pair <const Key, Type> > > class map;
/*
* 描述:模板类map 的构造函数
* 参数:
* @_Comp 函数对象类型,用来比较2 个元素之间的关系,map 依据此比较结果
进行排序
* @_Al 该map 对象使用的分配器
* @_Right 用于构造map 对象时复制的map 对象
* @_First 输入迭代器,起始位置
* @_Last 输入迭代器,结束位置,不含此位置
*/
map();
explicit map(const Traits& _Comp);
map(const Traits& _Comp, const Allocator& _Al);
map(const map<Key, Type, Traits, Allocator>& _Right);
template<class InputIterator> map(InputIterator _First, InputIterator _Last);
template<class InputIterator>
map(InputIterator _First, InputIterator _Last, const Traits& _Comp);
template<class InputIterator>
map(InputIterator _First, InputIterator _Last, const Traits& _Comp, const Allocator& _Al);
/*
* 描述: 模板类map 的析构函数
* 参数:
* 返回:
* 注意:
* 释放模板内所有元素并释放内存
*/
~map();
使用示例:
#include <cstdlib>
#include <iostream>
#include <map>
#include <functional>
using namespace std;
int main(int argc, char **argv)
{
typedef pair <int, int> Int_Pair;
map <int, int>::iterator m1_Iter, m3_Iter, m4_Iter, m5_Iter, m6_Iter;
map <int, int, greater<int> >::iterator m2_Iter;
// Create an empty map m0 of key type integer
map <int, int> m0;
// Create an empty map m1 with the key comparison
// function of less than, then insert 4 elements
map <int, int, less<int> > m1;
m1.insert( Int_Pair( 1, 10 ) );
m1.insert( Int_Pair( 2, 20 ) );
m1.insert( Int_Pair( 3, 30 ) );
m1.insert( Int_Pair( 4, 40 ) );
// Create an empty map m2 with the key comparison
// function of geater than, then insert 2 elements
map <int, int, greater<int> > m2;
m2.insert( Int_Pair( 1, 10 ) );
m2.insert( Int_Pair( 2, 20 ) );
// Create a map m3 with the
// allocator of map m1
map <int, int>::allocator_type m1_Alloc;
m1_Alloc = m1.get_allocator( );
map <int, int> m3( less<int>( ), m1_Alloc );
m3.insert( Int_Pair( 3, 30 ) );
// Create a copy, map m4, of map m1
map <int, int> m4( m1 );
// Create a map m5 by copying the range m1[_First, _Last)
map <int, int>::const_iterator m1_bcIter, m1_ecIter;
m1_bcIter = m1.begin( );
m1_ecIter = m1.begin( );
m1_ecIter++;
m1_ecIter++;
map <int, int> m5(m1_bcIter, m1_ecIter);
// Create a map m6 by copying the range m4[_First, _Last)
// and with the allocator of map m2
map <int, int>::allocator_type m2_Alloc;
m2_Alloc = m2.get_allocator( );
map <int, int> m6( m4.begin( ), ++m4.begin( ), less<int>( ), m2_Alloc);
cout << "m1 =";
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
cout << " " << m1_Iter -> second;
cout << endl;
cout << "m2 =";
for ( m2_Iter = m2.begin( ); m2_Iter != m2.end( ); m2_Iter++ )
cout << " " << m2_Iter -> second;
cout << endl;
cout << "m3 =";
for ( m3_Iter = m3.begin( ); m3_Iter != m3.end( ); m3_Iter++ )
cout << " " << m3_Iter -> second;
cout << endl;
cout << "m4 =";
for ( m4_Iter = m4.begin( ); m4_Iter != m4.end( ); m4_Iter++ )
cout << " " << m4_Iter -> second;
cout << endl;
cout << "m5 =";
for ( m5_Iter = m5.begin( ); m5_Iter != m5.end( ); m5_Iter++ )
cout << " " << m5_Iter -> second;
cout << endl;
cout << "m6 =";
for ( m6_Iter = m6.begin( ); m6_Iter != m6.end( ); m6_Iter++ )
cout << " " << m6_Iter -> second;
cout << endl;
return EXIT_SUCCESS;
}
输出:
m1 = 10 20 30 40
m2 = 20 10
m3 = 30
m4 = 10 20 30 40
m5 = 10 20
m6 = 10
/*
* 描述: 插入/访问元素
* 参数:
* 返回:
* 注意:
* 若key 不存在跑一次
*/
Type& operator[](const Key& _Key);
使用示例:
{//操作符[]
map<int, int> m0;
m0[0] = 5;
m0[1] = 6;
m0[2] = 7;
m0[3] = 8;
cout << "m0 =";
for (map<int, int>::const_iterator it = m0.begin( ); it != m0.end( ); ++it)
cout << " " << it -> second;
cout << endl;
}
输出:
m0 = 5 6 7 8
/*
* 描述:返回指向map 第一个元素的迭代器
* 参数:
* 返回:
* 指向map 第一个元素的迭代器
* 注意:
* 双向迭代器
*/
const_iterator begin() const;
iterator begin();
/*
* 描述:删除所有元素
* 参数:
* 返回:
* 注意:
* 删除元素的同时会释放所有已分配内存
*/
void clear( );
/*
* 描述:与指定key 值相等的元素个数
* 参数:
* @_Key 指定key 值
* 返回:
* 与指定key 值相等的元素个数
* 注意:
* 仅查询RB-tree 中[lower_bound(_key), upper_bound(_key))
*/
size_type count(const Key& _Key) const;
/*
* 描述:判断map 是否为空
* 参数:
* 返回:
* 空true,否则false
* 注意:
*/
bool empty( ) const;
/*
* 描述:返回指向map 最后一个元素的下一个位置的迭代器
* 参数:
* 返回:
* 指向map 最后一个元素的下一个位置的迭代器
*/
iterator end( );
const_iterator end( ) const;
/*
* 描述:返回pair 迭代器,
* 1. 指向第一个大于指定元素
* 2. 指向第一个等于或者大于指定元素
* 参数:
* @_Key 指定key 值
* 返回:
* 返回pair
* 注意:
* 1.不抛出异常
* 2.已有迭代器、引用不会失效除非该删除对象位置的元素
*/
pair <const_iterator, const_iterator> equal_range (const Key& _Key) const;
pair <iterator, iterator> equal_range (const Key& _Key);
/*
* 描述:删除指定位置元素或指定范围的元素
* 参数:
* @_Where 指向待删除元素迭代器
* @_First 指向待删除元素范围起始位置迭代器
* @_Last 指向待删除元素范围结束位置迭代器
* 返回:
* 指向被删除元素的下一个位置
* @size_type 返回删除个数
* 注意:
* 1.可能抛出“out_of_range”异常
* 2.已有迭代器、引用不会失效除非该删除对象位置的元素
*/
iterator erase(iterator _Where);
iterator erase(iterator _First, iterator _Last);
size_type erase(const key_type& _Key);
/*
* 描述:查找指定元素
* 参数:
* @_Key 指定key 值
* 返回:
* 元素位置
* 注意:
* 返回结果应与end()比较
*/
iterator find(const Key& _Key);
const_iterator find(const Key& _Key) const;
/*
* 描述:返回当前map 对象使用的分配器拷贝
* 参数:
* 返回:
* 当前map 对象使用的分配器拷贝
* 注意:
* 通常stl 库提供的分配器已经能满足使用
* 该接口一般而言用不到
*/
Allocator get_allocator( ) const;
/*
* 描述:插入一个元素
* 指定位置插入一个元素
* 插入指定返回元素(从另一个map 对象中导入)
* 参数:
* @_Val 插入元素的值
* @_Where 指向待插入位置的迭代器
* @_First 指向待拷贝元素范围起始位置迭代器
* @_Last 指向待拷贝元素范围结束位置迭代器
* 返回:
* 返回指向新元素的迭代器(pair)
* 注意:
* 注意返回值不同
*/
pair <iterator, bool> insert(const Type& _Val);
iterator insert(iterator _Where, const Type& _Val);
template<class InputIterator>
void insert(iterator _Where, InputIterator _First, InputIterator _Last);
/*
* 描述:返回比较函数对象(traits)
* 参数:
* 返回:
* 返回比较函数对象
* 注意:
* bool operator()(const Key& _xVal, const Key& _yVal);
* 当@_xVal 在@_yVal 之前且不等@_yVal 则true
*/
key_compare key_comp( ) const;
/*
* 描述:查找map 中等于或者第一个大于指定元素的迭代器
* 参数:
* @_Key 指定key 值
* 返回:
* map 中等于或者第一个大于指定元素的迭代器
* 注意:
* 返回结果应与end()比较
*/
const_iterator lower_bound(const Key& _Key) const;
iterator lower_bound(const Key& _Key);
/*
* 描述:返回deque 对象理论上可以保存最大元素个数
* 参数:
* 返回:
* deque 对象理论上可以保存最大元素个数
* 注意:
* 计算公式:size_t(-1)/sizeof(type)
* 该接口一般而言用不到
*/
size_type max_size( ) const;
/*
* 描述:返回指向map 最末尾哪个元素的迭代器
* 参数:
* 返回:
* 指向map 最末尾哪个元素的迭代器
* 注意:
* 进行比较以保证所获迭代器合法,若'rbegin() == rend()'则map 为空
*/
reverse_iterator rbegin( );
const_reverse_iterator rbegin( ) const;
/*
* 描述:返回指向map 最后一个元素下一位置的迭代器
* 参数:
* 返回:
* 指向map 最后一个元素下一位置的迭代器
* 注意:
*/
const_reverse_iterator rend( ) const;
reverse_iterator rend( );
/*
* 描述:返回map 元素个数
* 参数:
* 返回:
* 当前map 内元素个数
* 注意:
*/
size_type size( ) const;
/*
* 描述:交换两个map 对象
* 参数:
* @_Right 待交换的map 对象
* 返回:
* 注意:
*/
void swap(map<Key, Traits, Allocator>& _Right);
/*
* 描述:查找map 中第一个大于指定元素的迭代器
* 参数:
* @_Key 指定key 值
* 返回:
* map 中第一个大于指定元素的迭代器
* 注意:
* 返回结果应与end()比较
*/
const_iterator upper_bound(const Key& _Key) const;
iterator upper_bound(const Key& _Key);
/*
* 描述:返回比较函数对象(traits)
* 参数:
* 返回:
* 返回比较函数对象
* 注意:
* bool operator()(const Key& _xVal, const Key& _yVal);
* 当@_xVal 在@_yVal 之前且不等@_yVal 则true
* key_compare/value_compare 是同义次
*/
value_compare value_comp( ) const;
原创粉丝点击