栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++基础知识 - map

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++基础知识 - map

概述
  • map是典型的关联容器

    关联容器的特点是,按照关键字保存和访问。底层结构是用红黑树实现。

  • map还是有序容器

    默认情况下,标准库使用关键字类型的<运算符来比较两个关键字。所以,关键字的类型需要定义“正常行为”的<运算符。

    所有元素都会根据元素的关键字值自动排序。

  • map中所有元素都是pair

    pair中的第一个元素为key(关键字),起到索引的作用,第二个元素为vaule(值)。

  • 不重复

    map中不允许容器中有重复关键字元素。

创建
类别声明
empty (1)explicit map (const key_compare& comp = key_compare(), const allocator_type& alloc = allocator_type());
explicit map (const allocator_type& alloc);
range (2)template
map (InputIterator first, InputIterator last,
const key_compare& comp = key_compare(),
const allocator_type& = allocator_type());
copy (3)map (const map& x);
map (const map& x, const allocator_type& alloc);
move (4)map (map&& x);
map (map&& x, const allocator_type& alloc);
initializer list (5)map (initializer_list il,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());

创建一个空map

std::map map_; // 创建一个key类型为string,value类型为int的空map
查看 empty
bool empty() const noexcept;

查看map是否为空,size 为 0,则为true。建议使用该函数来查看map是否为,而不是比较size() == 0.

大小
size_type size() const noexcept;

size()返回该map中有多少个元素。

查找
iterator find (const key_type& k);
const_iterator find (const key_type& k) const;

查找某个关键字是否存在,存在则返回该元素对应的迭代器;不存在返回map::end

std::map map_;
const char *key = "test_key";
std::map::iterator it = map_.find(key);
if (it != map_.end()) {
    // found.
} else {
    // not found.
}

注意:

不推荐使用下标来查找,通过下标查找有一个严重的副作用:如果关键字还未在map中,下标操作会插入一个具有给定关键字的元素。

遍历

通过迭代器遍历map

std::map map_;
std::map::iterator it;
for (it = map_.begin(); it != map_.end(); ++it) {
    printf("key: %s, value: %dn", it->first.c_str(), it->second);
}
增加 insert
类别声明
single element (1)pair insert (const value_type& val);
template pair insert (P&& val);
with hint (2)iterator insert (const_iterator position, const value_type& val);
template iterator insert (const_iterator position, P&& val);
range (3)template
void insert (InputIterator first, InputIterator last);
initializer list (4)void insert (initializer_list il);

使用make_pair

std::map map_;
map_.insert(std::make_pair("key1", 1));

C++11中,创建一个pair最简单的方法是在参数列表中使用花括号初始化

std::map map_;
map_.insert({"key1", 1});

注意:

  • 不能修改

    insert插入时,如果该关键字已经存在,则不做任何操作,即不能修改该关键字对应的值。

  • 返回值

    insert返回一个pair,pair的first成员是一个迭代器,指向具有给定关键字的元素;second成员是一个bool值,指出元素是插入成功还是已经存在于容器中。如果关键字已在容器中,则insert什么事情也不做,且返回值中的bool部分为false。如果关键字不存在,元素被插入容器中,且bool值为true。

下标

使用下标[]插入元素

std::map map_;
map_["key1"] = 1;

下标插入的过程是:

如果容器中存在该关键字,则用该最新值更新原来的值;如果关键字不存在,则插入该元素。

删除 erase
iterator  erase (const_iterator position);
size_type erase (const key_type& k);
iterator  erase (const_iterator first, const_iterator last);

通过关键字删除

std::map map_;
const char *key = "key1";
map_.erase(key);

通过迭代器删除

std::map map_;
const char *key = "key1";
// 查找关键字是否存在
std::map::iterator it = map_.find(key);
if (it != map_.end()) {
    map_.erase(it); // 存在则删除
}

通过迭代器范围删除

std::map map_;
map_.erase(map_.begin(), map_.end()); // 删除整个map
clear
void clear() noexcept;

清空map,资源是否被释放,需要再研究看看。

修改

通过下标插入来修改

std::map map_;
const char *key = "key1";
int value = 1;
... ...
// 判断map_中是否包含某个关键字,存在则更新
std::map::iterator it = map_.find(key);
if (it != map_.end()) {
    map_[key] = value; // 通过下标的方式更新原有的值
}
参考
  • C++ primer 第五版
  • http://www.cplusplus.com/reference/map/map/?kw=map
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/628732.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号