C++STL之map学习

原创 2011年10月15日 23:38:59

map<Key, Data, Compare, Alloc>

 

map是一种关联容器,存储相结合形成一个关键和映射元素Map 是一种Pair Associative Container,意味着它的值类型为 pair<const Key, Data>. 而且也是 Unique Associative Container, 也就是任何两个元素没有相同的key值。

map具有重要的属性,就是在map对象中插入一个新元素指向现有元素迭代器失效map删除一个元素没有任何迭代器失效,除非,当然,实际上指向正在被删除元素迭代器

 

1、例子

struct ltstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) < 0;
  }
};

int main()
{
  map<const char*, int, ltstr> months;
  
  months["january"] = 31;
  months["february"] = 28;
  months["march"] = 31;
  months["april"] = 30;
  months["may"] = 31;
  months["june"] = 30;
  months["july"] = 31;
  months["august"] = 31;
  months["september"] = 30;
  months["october"] = 31;
  months["november"] = 30;
  months["december"] = 31;
  
  cout << "june -> " << months["june"] << endl;
  map<const char*, int, ltstr>::iterator cur  = months.find("june");
  map<const char*, int, ltstr>::iterator prev = cur;
  map<const char*, int, ltstr>::iterator next = cur;    
  ++next;
  --prev;
  cout << "Previous (in alphabetical order) is " << (*prev).first << endl;
  cout << "Next (in alphabetical order) is " << (*next).first << endl;
}

 

2、定义形式

template < class Key, class T, class Compare = less<Key>,
           class Allocator = allocator<pair<const Key,T> > > class map;

 

3、模板参数具有以下涵义

key关键类型在map对象中每个元素是通过该关键值唯一确定元素的
T:映射值类型在map中每个元素是用来储存一些数据作为其映射
compare:ComparisonA类键的类型,它有两个参数,并返回一个bool表达compA,Bcomp这比较A和B是关键对象返回true,如果早先的立场比B放置在一个严格弱排序操作可以是一个类实现一个函数调用运算符一个函数指针(见一个例子构造默认的对于<KEY>返回申请小于操作符相同默认值(A <B)。
Map对象使用这个表达式来确定在容器中元素位置以下这个规则在任何时候都排列map容器的所有元素
Allocator用于定义存储分配模型分配器对象类型默认情况下,分配器类模板它定义了最简单内存分配模式,是值独立的

 

map<Key,T>::iterator it;
(*it).first;             // 指向key值(of type Key)
(*it).second;            // 映射的值(of type T)
(*it);                   // the "element value" (of type pair<const Key,T>) 


也可以如下表达:

it->first;               // same as (*it).first   (the key value)
it->second;              // same as (*it).second  (the mapped value)


 

 

4、成员变量和成员函数

Member Where defined Description
key_type Associative Container map中的key类型
data_type Pair Associative Container key关联的值类型
value_type Pair Associative Container 对象类型, pair<const key_type, data_type>,存储在map中
key_compare Sorted Associative Container Function object 通过顺序比较
value_compare Sorted Associative Container Function object that compares two values for ordering.
pointer Container Pointer to T.
reference Container Reference to T
const_reference Container Const reference to T
size_type Container An unsigned integral type.
difference_type Container A signed integral type.
iterator Container Iterator used to iterate through a map. [1]
const_iterator Container Const iterator used to iterate through a map.
reverse_iterator Reversible Container Iterator used to iterate backwards through a map. [1]
const_reverse_iterator Reversible Container Const iterator used to iterate backwards through a map.
iterator begin() Container Returns an iterator pointing to the beginning of the map.
iterator end() Container Returns an iterator pointing to the end of the map.
const_iterator begin() const Container Returns a const_iterator pointing to the beginning of themap.
const_iterator end() const Container Returns a const_iterator pointing to the end of the map.
reverse_iterator rbegin() Reversible Container Returns a reverse_iterator pointing to the beginning of the reversed map.
reverse_iterator rend() Reversible Container Returns a reverse_iterator pointing to the end of the reversed map.
const_reverse_iterator rbegin() const Reversible Container Returns a const_reverse_iterator pointing to the beginning of the reversed map.
const_reverse_iterator rend() const Reversible Container Returns a const_reverse_iterator pointing to the end of the reversed map.
size_type size() const Container Returns the size of the map.
size_type max_size() const Container Returns the largest possible size of the map.
bool empty() const Container true if the map's size is 0.
key_compare key_comp() const Sorted Associative Container Returns the key_compare object used by the map.
value_compare value_comp() const Sorted Associative Container Returns the value_compare object used by the map.
map() Container Creates an empty map.
map(const key_compare& comp) Sorted Associative Container Creates an empty map, using comp as the key_compare object.
template <class InputIterator>
map(InputIterator f, InputIterator l)
Unique Sorted Associative Container Creates a map with a copy of a range.
template <class InputIterator>
map(InputIterator f, InputIterator l,
    const key_compare& comp)
Unique Sorted Associative Container Creates a map with a copy of a range, using comp as thekey_compare object.
map(const map&) Container The copy constructor.
map& operator=(const map&) Container The assignment operator
void swap(map&) Container Swaps the contents of two maps.
pair<iterator, bool>
insert(const value_type& x)
Unique Associative Container Inserts x into the map.
iterator insert(iterator pos,
                const value_type& x)
Unique Sorted Associative Container Inserts x into the map, using pos as a hint to where it will be inserted.
template <class InputIterator>
void insert(InputIterator, InputIterator)
[2]
Unique Sorted Associative Container Inserts a range into the map.
void erase(iterator pos) Associative Container Erases the element pointed to by pos.
size_type erase(const key_type& k) Associative Container Erases the element whose key is k.
void erase(iterator first, iterator last) Associative Container Erases all elements in a range.
void clear() Associative Container Erases all of the elements.
iterator find(const key_type& k) Associative Container Finds an element whose key is k.
const_iterator find(const key_type& k) const Associative Container Finds an element whose key is k.
size_type count(const key_type& k) Unique Associative Container Counts the number of elements whose key is k.
iterator lower_bound(const key_type& k) Sorted Associative Container Finds the first element whose key is not less than k.
const_iterator lower_bound(const key_type& k) const Sorted Associative Container Finds the first element whose key is not less than k.
iterator upper_bound(const key_type& k) Sorted Associative Container Finds the first element whose key greater than k.
const_iterator upper_bound(const key_type& k) const Sorted Associative Container Finds the first element whose key greater than k.
pair<iterator, iterator> 
equal_range(const key_type& k)
Sorted Associative Container Finds a range containing all elements whose key is k.
pair<const_iterator, const_iterator> 
equal_range(const key_type& k) const
Sorted Associative Container Finds a range containing all elements whose key is k.
data_type& 
operator[](const key_type& k) [3]
map See below.
bool operator==(const map&, 
                const map&)
Forward Container Tests two maps for equality. This is a global function, not a member function.
bool operator<(const map&, 
               const map&)
Forward Container Lexicographical comparison. This is a global function, not a member function.
 
下面展示了常用的一些方法。

// stu_map.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <map> using namespace std; bool fncomp(char lhs,char rhs) { return lhs<rhs; } struct classcomp { bool operator()(const char& lhs,const char& rhs) { return lhs<rhs; } }; int _tmain(int argc, _TCHAR* argv[]) { map<char,int> mymap; mymap['a']=10; mymap['b']=60; mymap['c']=30; mymap['d']=90; mymap['e']=50; map<char,int> second(mymap); map<char,int> third(mymap.begin(),mymap.end()); map<char,int,classcomp> fourth; bool(*fn_pt)(char,char)=fncomp; map<char,int,bool(*)(char,char)> fifth(fn_pt); map<char,int>::key_compare key_comp; map<char,int>::iterator it; it=mymap.begin(); for (it;it!=mymap.end();it++) { cout<<it->first<<":"<<it->second<<endl; } cout<<"================================="<<endl; second.clear(); second['a']=1002; second['b']=10023; while (!second.empty()) { cout << second.begin()->first << " => "; cout << second.begin()->second << endl; second.erase(second.begin()); } cout<<"================================="<<endl; mymap.insert(pair<char,int>('f',100) ); mymap.insert(pair<char,int>('g',200) ); cout<<"f => " <<mymap.find('f')->second<<endl; cout<<"g => " <<mymap.find('g')->second<<endl; cout<<"================================="<<endl; key_comp=mymap.key_comp(); cout << "mymap contains:\n"; char highest=mymap.rbegin()->first; // key value of last element it=mymap.begin(); do { cout << (*it).first << " => " << (*it).second << endl; } while ( key_comp((*it++).first, highest) ); cout << endl; return 0; }


运行结果:

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wallwind/article/details/6876892

【曾贤志】Power Map For Excel地图图表视频教程

-
  • 1970年01月01日 08:00

C++ STL 中 map 容器的说明和使用技巧

Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!    1、map简介 map是一类关联...
  • u014552726
  • u014552726
  • 2014-07-22 10:04:02
  • 9622

STL学习之map容器(3)

map::find iterator find ( const key_type& x ); const iterator find( const key_type& x) const; 获取...
  • richerg85
  • richerg85
  • 2012-06-24 10:32:25
  • 7259

HashMap的containsKey方法说明

/** * Returns the entry associated with the specified key in the * HashMap. Returns null ...
  • threedonkey
  • threedonkey
  • 2014-08-22 22:21:52
  • 8301

C++基础:C++标准库之map简介

1、综述         Map是C++STL中众多的Container(容器)之一,与python的字典略类似,Map作为一个关联容器,将key与value相互关联,其中key为关键字,是不可更改...
  • zhangyalin1992
  • zhangyalin1992
  • 2014-05-19 22:35:32
  • 1465

map.containsKey 判断map集合中是否存在相同的健名

       故事的背景是这样的:有了问题没有思路了,就想着问,所以现在常说的一句话就是:组长,这个怎么办。当然这次也不例外,因为遇到了标签字符串,中间用逗号隔开,然后现在的需求是,如果标签列表中包含...
  • zhang18330699274
  • zhang18330699274
  • 2018-03-06 10:23:57
  • 75

C++ STL中Map的按Key排序和按Value排序

C++ STL中Map的按Key排序和按Value排序, 实用方法小结!
  • IIcyZhao
  • IIcyZhao
  • 2013-09-22 18:29:25
  • 51989

C++ map迭代器的基本操作和使用

1、map简介 map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。对于迭代器来说,可以修改实值,而不能修改key。 2、ma...
  • cyangc
  • cyangc
  • 2014-04-14 14:14:54
  • 10395

STL中map用法详解

Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候...
  • bat603
  • bat603
  • 2006-12-23 15:49:00
  • 104733

小心使用STL中map的[]操作符

    一个map就是一个(关键码(key),值(value))对偶的序列,它提供基于关键码的快速提取操作。也就是说,可以用下标运算符[]将关键码作为下标去执行查找,并返回对应的值。因此可以把map的...
  • starlee
  • starlee
  • 2008-04-07 09:26:00
  • 19539
收藏助手
不良信息举报
您举报文章:C++STL之map学习
举报原因:
原因补充:

(最多只允许输入30个字)