我们都知道map的底层结构是红黑树,映射是关联容器,map中的元素是一些关键字-值对:关键字起到索引的作用,值则表示与索引向关联的数据。

template < class Key,                                     // map::key_type
           class T,                                       // map::mapped_type
           class Compare = less<Key>,                     // map::key_compare
           class Alloc = allocator<pair<const Key,T> >    // map::allocator_type
           > class map;

key

键的类型。映射中的每个元素都由其键值惟一标识。

别名为成员类型映射::key_type。

T

映射值的类型。映射中的每个元素都将一些数据存储为其映射值。

别名为成员类型映射::mapped_type。

Alloc

用于定义存储分配模型的分配器对象的类型。默认情况下,使用的是分配器类模板,它定义了最简单的内存分配模型,并且是独立于值的。

别名为成员类型映射::allocator_type

成员类型映射:

1、key_type:第一个模板参数(键)。
2、mapped_type:第二个模板参数(T)。
3、value_type:pair<const key_type,mapped_type>

迭代器:

1、begin : 返回迭代器到开始(公共成员函数)
2、end:返回迭代器到末尾(公共成员函数)
3、rbegin:返回反向迭代器到反向开始(公共成员函数)
4、rend:返回反向迭代器到反向端(公共成员函数)
5、cbegin:将const_iterator返回到开始(公共成员函数)
6、cend:返回const_iterator末尾(公共成员函数)
7、crbegin:返回const_reverse_iterator到反向开始(公共成员函数)
8、crend:返回const_reverse_iterator到reverse end(公共成员函数)

begin和end在map中的用法:

#include <iostream>
#include <map>
using namespace std;

int main (int argc, char **argv)
{
  map<char,int> mymap;
  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;

   for (auto it = mymap.begin(); it != mymap.end(); ++it)
   {
   		cout <<"first:" <<it->first<<" " <<it->second<<endl;
   }
  return 0;
}

输出结果:

java 输出map的key和value 输出map内容_成员函数

rbegib()和rend()在map中。如果map对象是常量限定的,函数返回一个const_reverse_iterator。否则,它将返回一个reverse_iterator。成员类型reverse_iterator和const_reverse_iterator是指向元素的反向双向迭代器类型。代码如下:

#include <iostream>
#include <map>
using namespace std;
int main (int argc, char **argv)
{
  map<char,int> mymap;
  mymap['a'] = 100;
  mymap['b'] = 200;
  mymap['c'] = 300;

  //std::map<char, int>::reverse_iterator  rit;
   for ( auto rit = mymap.rbegin(); rit != mymap.rend(); ++rit)
   {
   		cout <<"first:" <<rit->first<<" " <<rit->second<<endl;
   }
  return 0;
}

运行结果如下:

java 输出map的key和value 输出map内容_成员函数_02

后面两组迭代器代码上还是一样的,我就不写了;

容量:

1、empty:测试容器是否为空(公共成员函数)
2、size:返回容器大小(公共成员函数)
3、max_size:返回最大大小(公共成员函数)

empty()返回映射中是否为空,再上面的例子上稍微改一下:

#include <iostream>
#include <map>
using namespace std;
int main (int argc, char **argv)
{
  map<char,int> mymap;
  mymap['a'] = 100;
  mymap['b'] = 200;
  mymap['c'] = 300;
  while (!mymap.empty())
  {  
	cout << mymap.begin()->first << "  " << mymap.begin()->second << endl;
    mymap.erase(mymap.begin());   
  }
  return 0;
}
输出结果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/201908222124016.png)size()返回映射容器中的元素数量。代码如下:
#include <iostream>
#include <map>
using namespace std;
int main (int argc, char **argv)
{
  map<char,int> mymap;
  mymap['a'] = 100;
  mymap['b'] = 200;
  mymap['c'] = 300;
  cout <<"size:"<<mymap.size()<<endl;
  return 0;
}

输出结果:

java 输出map的key和value 输出map内容_成员函数_03

max_size()返回映射容器可以容纳的元素的最大数量。代码如下:

#include <iostream>
#include <map>
using namespace std;

int main (int argc, char **argv)
{
  map<int,int> mymap;
  if (mymap.max_size() > 2000)
  {
  		for(int i=0; i < 2000; i++)
  			mymap[i] = 0;//都初始化为0
  		cout <<"It can hold 2000"<<endl;
  }
  else
  {
  	cout <<"It can't hold 2000"<<endl;
  }
  return 0;
}

输出结果:

java 输出map的key和value 输出map内容_成员函数_04

元素访问

1、operator[] : 访问元素(公共成员函数)
2、at:访问元素(公共成员函数)

3、operator[]:
4、mapped_type& operator[] (const key_type& k);
5、mapped_type& operator[] (key_type&& k);

如果k匹配容器中元素的键,则函数返回对其映射值的引用。代码如下:

#include <iostream>
#include <map>
using namespace std;

int main (int argc, char **argv)
{
  map<char,string> mymap;

  mymap['a'] = "an element";
  mymap['b'] = "another element";
  mymap['c'] = mymap['b'];

  cout << "mymap['a'] is " << mymap['a'] << endl;
  cout << "mymap['b'] is " << mymap['b'] << endl;
  cout << "mymap['c'] is " << mymap['c'] << endl;
  cout << "mymap['d'] is " << mymap['d'] << endl;

  return 0;
}

输出结果:

java 输出map的key和value 输出map内容_ios_05

at:

1、mapped_type& at (const key_type& k);
2、const mapped_type& at (const key_type& k) const;

返回对键k标识的元素的映射值的引用。代码如下:

#include <iostream>
#include <map>
using namespace std;

int main (int argc, char **argv)
{
  map<string,int> mymap= {
                { "xiaozhang", 0 },
                { "xiaowang", 0 },
                { "xiaoli", 0 } };

  mymap.at("xiaozhang") = 10;
  mymap.at("xiaowang") = 20;
  mymap.at("xiaoli") = 30;

  for (auto i = mymap.begin(); i != mymap.end(); ++i)
  {
  		cout <<i->first<<" " << i->second << endl;
  }
  return 0;
}

输出结果:

java 输出map的key和value 输出map内容_ios_06

修改器:

1、insert:插入元素(公共成员函数)
2、erase:删除元素(公共成员函数)
3、swap:交换内容(公共成员函数)
4、clear:清除内容(公众成员功能)

insert()通过插入新元素扩展容器,通过插入的元素数量有效地增加容器大小。

(1)pair<iterator,bool> insert (const value_type& val);
template <class P> pair<iterator,bool> insert (P&& val);
(2)iterator insert (const_iterator position, const value_type& val);
template <class P> iterator insert (const_iterator position, P&& val);
(3)template <class InputIterator>
void insert (InputIterator first, InputIterator last);
(4)void insert (initializer_list<value_type> il);

void insert (initializer_list<value_type> il);

val:要复制到(或作为)插入元素移动的值。成员类型value_type是容器中元素的类型,在map中定义pair<const key_type,mapped_type>,代码如下:

#include <iostream>
#include <map>
using namespace std;

int main (int argc, char **argv)
{
 std::map<char,int> mymap;

  // (1)
  mymap.insert ( pair<char,int>('a',100) );
  mymap.insert ( pair<char,int>('z',200) );

  pair<map<char,int>::iterator,bool> ret;
  ret = mymap.insert ( pair<char,int>('z',500) );
  if (ret.second==false)
  {
    cout << "element 'z' already existed";
    cout << " with a value of " << ret.first->second << endl;
  }

  // (2)
  map<char,int>::iterator it = mymap.begin();
  mymap.insert (it, pair<char,int>('b',300)); 
  mymap.insert (it, pair<char,int>('c',400));  

  // (3)
  map<char,int> anothermap;
  anothermap.insert(mymap.begin(),mymap.find('c'));

  // (4)
  cout << "mymap contains:"<<endl;
  for (it=mymap.begin(); it!=mymap.end(); ++it)
    cout << it->first << " = " << it->second << endl;

  cout << "anothermap contains:"<<endl;
  for (it=anothermap.begin(); it!=anothermap.end(); ++it)
    cout << it->first << " = " << it->second << endl;

  return 0;
}

输出结果:

java 输出map的key和value 输出map内容_成员函数_07

erase()从映射容器中删除单个元素或元素范围([first,last])。这有效地减少了容器的大小,减少了被删除的元素的数量。

(1)	iterator  erase (const_iterator position);
(2)	size_type erase (const key_type& k);
(3)	iterator  erase (const_iterator first, const_iterator last);

举例:

#include <iostream>
#include <map>
using namespace std;

int main (int argc, char **argv)
{
  map<char,int> mymap;
  map<char,int>::iterator it;

  mymap['a']=10;
  mymap['b']=20;
  mymap['c']=30;
  mymap['d']=40;
  mymap['e']=50;
  mymap['f']=60;

  it=mymap.find('b');
  mymap.erase (it);                   // erasing by iterator

  mymap.erase ('c');                  // erasing by key

  it=mymap.find ('e');
  mymap.erase ( it, mymap.end() );    // erasing by range

  for (it=mymap.begin(); it!=mymap.end(); ++it)
    cout << it->first << " => " << it->second << endl;


  return 0;
}

输出结果:

java 输出map的key和value 输出map内容_ios_08

swap():

void swap (map& x);

通过x的内容交换容器的内容,x是另一个相同类型的映射。大小可能不同。代码如下:

#include <iostream>
#include <map>
using namespace std;

int main (int argc, char **argv)
{
  map<char,int> foo,bar;

  foo['x']=100;
  foo['y']=200;

  bar['a']=11;
  bar['b']=22;
  bar['c']=33;

  foo.swap(bar);

  cout << "foo contains:"<<endl;
  for (map<char,int>::iterator it=foo.begin(); it!=foo.end(); ++it)
    cout << it->first << "=" << it->second <<endl;

 cout << "bar contains:"<<endl;
  for (map<char,int>::iterator it=bar.begin(); it!=bar.end(); ++it)
    cout << it->first << "=" << it->second << endl;
  return 0;
}

输出结果:

java 输出map的key和value 输出map内容_#include_09

void clear() noexcept;

从map容器中删除所有元素(已销毁),使容器的大小为0。代码如下:

#include <iostream>
#include <map>
using namespace std;

int main (int argc, char **argv)
{
  map<char,int> mymap;

  mymap['x']=100;
  mymap['y']=200;
  mymap['z']=300;

  cout << "mymap contains:\n";
  for (map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    cout << it->first << " => " << it->second << endl;

  mymap.clear();
  mymap['a']=1101;
  mymap['b']=2202;

  cout << "mymap contains:\n";
  for (map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    cout << it->first << " => " << it->second << endl;

  return 0;
}

输出结果:

java 输出map的key和value 输出map内容_#include_10

操作:

1、find: 获取元素的迭代器(公共成员函数)
2、count:使用特定键数元素(公共成员函数)
3、lower_bound:将迭代器返回到下界(公共成员函数)
4、upper_bound:将迭代器返回到上界(公共成员函数)
5、equal_range:获取相等元素的范围(公共成员函数)
6、find: iterator find (const key_type& k);
7、const_iterator find (const key_type& k) const;

在容器中搜索键值等于k的元素,如果找到,返回一个迭代器,否则返回一个迭代器map::end。代码如下:

#include <iostream>
#include <map>
using namespace std;

int main (int argc, char **argv)
{
  map<char,int> mymap;
  map<char,int>::iterator it;

  mymap['a']=50;
  mymap['b']=100;
  mymap['c']=150;
  mymap['d']=200;

  it = mymap.find('b');
  if (it != mymap.end()) //如果找到b则删除
    mymap.erase (it);

  // print content:
  cout << "elements in mymap:" << '\n';
  cout << "a => " << mymap.find('a')->second << '\n';
  cout << "c => " << mymap.find('c')->second << '\n';
  cout << "d => " << mymap.find('d')->second << '\n';

  return 0;
}

输出结果:

java 输出map的key和value 输出map内容_ios_11

count(): size_type count (const key_type& k) const;

在容器中搜索键值等于k的元素,并返回匹配项的数量。代码如下:

#include <iostream>
#include <map>
using namespace std;

int main (int argc, char **argv)
{
  map<char,int> mymap;
  char c;

  mymap ['a']=101;
  mymap ['c']=202;
  mymap ['f']=303;

  for (c='a'; c<'h'; c++)
  {
    std::cout << c;
    if (mymap.count(c)>0)
      cout << " is an element of mymap.\n";
    else 
      cout << " is not an element of mymap.\n";
  }

  return 0;
}

输出结果:

java 输出map的key和value 输出map内容_ios_12

lower_bound和upper_bound :

(1)iterator lower_bound (const key_type& k);
const_iterator lower_bound (const key_type& k) const;
返回一个迭代器,该迭代器指向容器中的第一个元素,该元素的键不在k之前。
(2)iterator upper_bound (const key_type& k);
const_iterator upper_bound (const key_type& k) const;

返回一个迭代器,该迭代器指向容器中的第一个元素,该元素的键被认为位于k之后。
代码如下:

#include <iostream>
#include <map>
using namespace std;

int main (int argc, char **argv)
{
  map<char,int> mymap;
  map<char,int>::iterator itlow,itup;

  mymap['a']=20;
  mymap['b']=40;
  mymap['c']=60;
  mymap['d']=80;
  mymap['e']=100;

  itlow=mymap.lower_bound ('b');  // itlow points to b
  itup=mymap.upper_bound ('d');   // itup points to e (not d!)

  mymap.erase(itlow,itup);        // erases [itlow,itup)

  // print content:
  for (map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    cout << it->first << " => " << it->second << '\n';

  return 0;
}

输出结果:

java 输出map的key和value 输出map内容_ios_13