STL C++ Map使用

  1. Map内部结构

map內部的实现自建一颗红黑树,这颗树具有对数据自动排序的功能。

//1、map这里指定less作为其默认比较函数(对象),就是默认按键值升序排列
// map<string, int> name_score_map;

// 2、可以自定义,按照键值升序排列,注意加载 
// #include <functional> // std::greater
// map<string, int, greater<string>> name_score_map;


//3、按照自定义内容进行排序,比如字符串的长度
map<string, int, CmpByKeyLength> name_score_map;

//cmp定义如下
struct CmpByKeyLength {
bool operator()(const string& k1, const string& k2)const {
    return k1.length() < k2.length();
}
};
  1. 插入元素

// 定义一个map对象
map<int, string> mapStudent;

// 第一种 用insert函數插入pair
mapStudent.insert(pair<int, string>(000, “student_zero”));

// 第二种 用insert函数插入value_type数据
mapStudent.insert(map<int, string>::value_type(001, “student_one”));

// 第三种 用"array"方式插入
mapStudent[123] = “student_first”;
mapStudent[456] = “student_second”;
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的 插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是不能在插入数据的,但是用数组方式就不同了,它可以覆盖以前该关键字对 应的值,用程序说明如下:

mapStudent.insert(map<int, string>::value_type (001, “student_one”));
mapStudent.insert(map<int, string>::value_type (001, “student_two”));

上面这两条语句执行后,map中001这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下

// 构造定义,返回一个pair对象
pair<iterator,bool> insert (const value_type& val);
 
pair<map<int, string>::iterator, bool> Insert_Pair;
 
Insert_Pair = mapStudent.insert(map<int, string>::value_type (001, "student_one"));
 
if(!Insert_Pair.second)
    cout << ""Error insert new element" << endl;

我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。

3.查找元素

当所查找的关键key出现时,它返回数据所在对象的位置,如果沒有,返回iter与end函数的值相同。
如果使用find函数,需要判断迭代器是否为end()
std::map::end说明:
The past-the-end element is the theoretical element that would follow the last element in the map container. It does not point to any element, and thus shall not be dereferenced.

// find 返回迭代器指向当前查找元素的位置否则返回map::end()位置
iter = mapStudent.find("123");
 
if(iter != mapStudent.end())
       cout<<"Find, the value is"<<iter->second<<endl;
else
   cout<<"Do not Find"<<endl;

4.删除

//迭代器刪除
iter = mapStudent.find(“123”);
mapStudent.erase(iter);

//用关键字刪除
int n = mapStudent.erase(“123”); //如果刪除了會返回1,否則返回0

//用迭代器范围刪除 : 把整个map清空
mapStudent.erase(mapStudent.begin(), mapStudent.end());
//等同于mapStudent.clear()

5.map的大小

int nSize = mapStudent.size();

9,map的基本操作函数:

 C++ maps是一种关联式容器,包含“关键字/值”对

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

 clear()        删除所有元素

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

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

 end()           返回指向map末尾的迭代器

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

 erase()         删除一个元素

 find()          查找一个元素

 get_allocator() 返回map的配置器

 insert()        插入元素

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

 lower_bound()   返回键值>=给定元素的第一个位置,未找到,返回end()

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

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

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

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

 swap()           交换两个map

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

 value_comp()     返回比较元素value的函数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值