C/C++ | STL map的基本函数和用法 | 注:绝对简洁不是长篇大论

版权声明:本人小白,有错误之处恳请指出,感激不尽;欢迎转载 https://blog.csdn.net/stone_fall/article/details/88669763

Map

在机试中常用到的标准模板库。我将在展示常用操作的过程中展示其库函数用法。

主要特点

  1. C++ Maps是一种关联式容器,包含“关键字/值”对。关键字只得出现一次(主键,非重复)。
  2. map內部的实现自建一颗红黑树,这颗树具有对数据自动排序的功能。根据key值快速查找记录,查找的复杂度基本是Log(N)

目录

Map

主要特点

插入元素

insert() 插入元素

数组方式插入

遍历

begin() / end() 返回指向map头部/尾部的迭代器

rbegin() / rend() 返回一个指向map尾部/头部的逆向迭代器

size() 返回map中元素的个数

判断元素是否存在

count() 返回指定元素出现的次数

find() 查找一个元素

lower_bound() / upper_bound() 返回键值>=/>给定元素的第一个位置

equal_range() 返回特殊条目的迭代器对

删除所有元素-clear()

erase() 删除一个元素

empty() 如果map为空则返回true

max_size() 返回可以容纳的最大元素个数

swap() 交换两个map

value_comp() 返回比较元素value的函数

key_comp() 返回比较元素key的函数

get_allocator() 返回map的配置器


插入元素

insert() 插入元素

	map<int,int> intMap;
	//若有对应关键字,不成功 
	intMap.insert(pair<int ,int>(1,1));
	intMap.insert(map<int,int>::value_type(2,2));

数组方式插入

	//数组是覆盖对应关键字key的值value 
	intMap[3]=3;
        //范围插入 
	map<int,int> tmp;
	tmp.insert(intMap.begin(),intMap.end());

遍历

注意区别!!!

begin() / end() 返回指向map头部/尾部的迭代器

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

输出:

1 1
2 2
3 3

rbegin() / rend() 返回一个指向map尾部/头部的逆向迭代器

	map<int,int>::reverse_iterator rit;
	for(rit = intMap.rbegin();rit!=intMap.rend();rit++){
		cout<<rit->first<<" "<<rit->second<<endl;
	} 

输出:

3 3
2 2
1 1

size() 返回map中元素的个数

	int size=tmp.size();
	for(int i=1;i<=size;i++){
		cout<<i<<" "<<tmp[i]<<endl;
	}

输出:

1 1
2 2
3 3

判断元素是否存在

count() 返回指定元素出现的次数

count()函数返回map中键值等于key的元素的个数。由于不会出现重复元素,这里只会返回0/1!!!

	int k=intMap.count(1);

find() 查找一个元素

find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器

	map<int,int>::iterator it;
	it = intMap.find(3);
	if(it == intMap.end()){
		cout<<"Not Find!"<<endl; 
	}
	else{
		cout<<it->first<<" "<<it->second<<endl; 
	}

lower_bound() / upper_bound() 返回键值>=/>给定元素的第一个位置

不常用的函数

假设只插入了上文所述的键值对

	map<int,int>::iterator low,up;
	low=intMap.lower_bound(3);
	up=intMap.upper_bound(3);
	if(low!=up)cout<<"Find"<<endl;
	else cout<<"Not Find"<<endl;
	low=intMap.lower_bound(4);
	up=intMap.upper_bound(4);
	if(low!=up)cout<<"Find"<<endl;
	else cout<<"Not Find"<<endl;

输出:

Find
Not Find

equal_range() 返回特殊条目的迭代器对

equal_range()函数返回两个迭代器——一个指向第一个键值为key的元素,另一个指向最后一个键值为key的元素。

    pair<map<int, string>::iterator, map<int, string>::iterator> mappair;  
  
    mappair = mapStudent.equal_range(2);  
  
    if(mappair.first == mappair.second)  
  
        cout<<"Do not Find"<<endl;  
  
    else  
  
        cout<<"Find"<<endl;  

删除所有元素-clear()

常在初始化中使用,clear()函数删除map中的所有元素

	intMap.clear();

erase() 删除一个元素

  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
  mymap.erase( mymap.begin(), mymap.end() );   //erasing all

empty() 如果map为空则返回true

empty()函数返回真(true)如果map为空,否则返回假(false)。

  map<char,int> Cmap;

  Cmap['a']=10;
  Cmap['b']=20;
  Cmap['c']=30;

  while (!Cmap.empty())
  {
    cout << Cmap.begin()->first << " => " << Cmap.begin()->second << '\n';
    mymap.erase(Cmap.begin());
  }

max_size() 返回可以容纳的最大元素个数

用于预先检查映射是否允许插入1000个元素。

  int i;
  std::map<int,int> mymap;

  if (mymap.max_size()>1000)
  {
    for (i=0; i<1000; i++) mymap[i]=0;
    cout << "contains 1000 elements.\n";
  }
  else cout << "not hold 1000 elements.\n";

swap() 交换两个map

交换两个map中的元素

  std::map<char,int> foo,bar;

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

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

  foo.swap(bar);

下面几个没用过

value_comp() 返回比较元素value的函数

value_comp()函数返回一个比较元素value的函数。

key_comp() 返回比较元素key的函数

key_comp()函数返回一个比较key的函数。

get_allocator() 返回map的配置器

get_allocator()函数返回map的配置器。

返回与映射关联的分配器对象的副本。

猜你喜欢

转载自blog.csdn.net/stone_fall/article/details/88669763