映射容器map是一种关联容器,表示的是对偶(键,值)的序列。它支持唯一Key类型的键值,并提供对另一个基于键的类型T的快速检索。map还提供双向迭代器。映射容器map在标准C++中,对应于map类,被定义在<map>头文件中。
映射:键→值,值 = 映射[键](似f:x → y,y = f(x))。即,可以通过映射,由键来快速定位值。
namespace std { // 取自C++2003标准
template <class Key, class T, class Compare = less<Key>, class Allocator = allocator< pair<const Key, T> > > class map {
public:
// types:类型
typedef Key key_type;
typedef T mapped_type;
typedef pair<const Key, T> value_type;
typedef Compare key_compare;
typedef Allocator allocator_type;
typedef typename Allocator::reference reference;
typedef typename Allocator::const_reference const_reference;
typedef implementation defined iterator;
typedef implementation defined const_iterator;
typedef implementation defined size_type;
typedef implementation defined difference_type;
typedef typename Allocator::pointer pointer;
typedef typename Allocator::const_pointer const_pointer;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
class value_compare : public binary_function<value_type,value_type,bool> {
friend class map;
protected:
Compare comp;
value_compare(Compare c) : comp(c) {}
public:
bool operator()(const value_type& x, const value_type& y) const {
return comp(x.first, y.first);
}
};
// construct/copy/destroy:构造/拷贝/销毁
explicit map(const Compare& comp = Compare(), const Allocator& = Allocator());
template <class InputIterator> map(InputIterator first, InputIterator last, const Compare& comp = Compare(), const Allocator& = Allocator());
map(const map<Key,T,Compare,Allocator>& x);
˜map();
map<Key,T,Compare,Allocator>& operator=(const map<Key,T,Compare,Allocator>& x);
allocator_type get_allocator() const;
// iterators:迭代器
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
reverse_iterator rend();
const_reverse_iterator rend() const;
// capacity:容量
bool empty() const;
size_type size() const;
size_type max_size() const;
// element access:
T& operator[](const key_type& x);
// modifiers:修改方法
pair<iterator, bool> insert(const value_type& x);
iterator insert(iterator position, const value_type& x);
template <class InputIterator> void insert(InputIterator first, InputIterator last);
void erase(iterator position);
size_type erase(const key_type& x);
void erase(iterator first, iterator last);
void swap(map<Key,T,Compare,Allocator>&);
void clear();
// observers:观测器
key_compare key_comp() const;
value_compare value_comp() const;
// map operations:映射操作
iterator find(const key_type& x);
const_iterator find(const key_type& x) const;
size_type count(const key_type& x) const;
iterator lower_bound(const key_type& x);
const_iterator lower_bound(const key_type& x) const;
iterator upper_bound(const key_type& x);
const_iterator upper_bound(const key_type& x) const;
pair<iterator,iterator>
equal_range(const key_type& x);
pair<const_iterator,const_iterator>
equal_range(const key_type& x) const;
};
// 比较运算符重载:
template <class Key, class T, class Compare, class Allocator> bool operator==(const map<Key,T,Compare,Allocator>& x, const map<Key,T,Compare,Allocator>& y);
template <class Key, class T, class Compare, class Allocator> bool operator< (const map<Key,T,Compare,Allocator>& x, const map<Key,T,Compare,Allocator>& y);
template <class Key, class T, class Compare, class Allocator> bool operator!=(const map<Key,T,Compare,Allocator>& x, const map<Key,T,Compare,Allocator>& y);
template <class Key, class T, class Compare, class Allocator> bool operator> (const map<Key,T,Compare,Allocator>& x, const map<Key,T,Compare,Allocator>& y);
template <class Key, class T, class Compare, class Allocator> bool operator>=(const map<Key,T,Compare,Allocator>& x, const map<Key,T,Compare,Allocator>& y);
template <class Key, class T, class Compare, class Allocator> bool operator<=(const map<Key,T,Compare,Allocator>& x, const map<Key,T,Compare,Allocator>& y);
// specialized algorithms:专用算法
template <class Key, class T, class Compare, class Allocator>
void swap(map<Key,T,Compare,Allocator>& x, map<Key,T,Compare,Allocator>& y);
}
map容器的值类型是pair:
typedef pair<const Key, T> value_type;
其中的pair是标准C++定义在<utility>头文件中的模版结构:
template<class Type1, class Type2> struct pair {
typedef Type1 first_type;
typedef Type2 second_type
Type1 first;
Type2 second;
pair( );
pair(const Type1& __Val1, const Type2& __Val2);
template<class Other1, class Other2> pair(const pair<Other1, Other2>& _Right);
};
为了显示映射的威力,我们举一个单词计数程序的例子:(此处的单词是以白空符分隔的广义字符串)
#include < iostream >
#include < fstream >
#include < map >
#include < string >
using namespace std ;
int main ( int argc , char * argv [ ] ) {
typedef map < string , int > WordMap ; // 定义特定的单词映射类型
typedef WordMap :: iterator wmIter ; // 定义该类型的迭代器
const char* fname = "WordCount.cpp" ; // 缺省文件名串
if ( argc > 1 ) fname = argv [ 1 ] ; // 读入命令行的第一个参数,作为文件名路径串
ifstream in ( fname ) ; // 打开文件输入流
if ( ! in ) { // 如果打开错误,则显示提示信息后退出
cout << " Open file " << fname << " error ! " << endl ;
return 1 ;
}
WordMap wordmap ; // 定义单词映射对象
string word ; // 定义单词字符串对象
while ( in >> word ) wordmap [ word ] ++ ; // 从文件中读入单词,
// 在wordmap [ word ]第一次出现时,
// 将单词word与其映射值wordmap之对偶元素(word,wordmap)
// 加入容器,并将映射wordmap的值加一(初值为0)。
// 遍历容器,显示输出计数大于等于3的单词和计数
for ( wmIter w = wordmap . begin ( ) ; w != wordmap . end ( ) ; w ++ )
if ( w->second >= 3 ) cout << w->first << " : " << w->second << endl ;
// 显示单词串“wordmap”的计数值
// cout << " wordmap : " << wordmap [ "wordmap" ] << endl ;
return 0 ;
}
此例中,“值 = 映射[键]”对应于“计数 = 映射[单词]”,即“count = wordmap [ word ]”。因为map容器中的内容是按键从小到大顺序排列的,所以在wordmap容器中的映射元素(即键值对<word, count>)是按单词的ASCII码字典顺序排列的。
程序中的 wordmap[word]++ ; 对(将int与word关联在一起的映射值)表达式进行增1操作。如果映射中没有单词word,则作为该单词的键-值对(此时的值被初始化为0),就会被自动加入到映射中(这是map容器所具有的零初始化能力);如果word已经在映射中,则该表达式会将键所对应的值加1。
你也可以用下面的语句,来单独显示某个单词键(例如单词字符串“wordmap”)的计数值:
cout << " wordmap : " << wordmap [ "wordmap" ] << endl ;
运行结果:
width="728" scrolling="no" height="90" frameborder="0" align="middle" src="http://download1.csdn.net/down3/20070601/01184120111.htm" marginheight="0" marginwidth="0">