C++ std::map 的使用

注1:参考 cppreference.com 内容,这里对 cppreference.com 中的内容做实例举证

注2:本文基于 C++17

一、 std::map 是有序的

1.1 std::map 默认排序

std::map 是一种有序关联容器,键之间以函数 Compare 排序。

std::map 默认的迭代器以升序为迭代器,此升序由构造时所用的比较函数定义。

如下为 std::map 的默认:

int main() {
    std::map<int, std::string> mapDebug;
    mapDebug.insert({30, "abc"});
    mapDebug.insert({4, "abddc"});
    mapDebug.insert({7, "afsdfdsbc"});
    mapDebug.insert({1, "haha"});

    for (auto curKV : mapDebug) {
        std::cout << curKV.first << ":" << curKV.second << "; ";
    }
    std::cout << std::endl;
}

// 输出如下:
// 1:haha; 4:abddc; 7:afsdfdsbc; 30:abc; 

1.2 std::map 自定义排序

如果是自定义的数据结构作为 key,则需要重新写比较函数,要不然会报错,如下:

struct DataInfo {
    int numKey;
    char charKey;
    std::string strKey;
    DataInfo(int a, char b, std::string c) : 
        numKey(a), charKey(b), strKey(c) {};
};

struct CmpKey {
    bool operator() (const DataInfo &a, const DataInfo &b) const
    {
        if (a.numKey == b.numKey) {
            if (a.charKey == b.charKey) {
                return a.strKey < b.strKey;  // strKey 升序;
            }
            return a.charKey > b.charKey;  // charKey 降序;
        }
        return a.numKey < b.numKey;  // numKey 升序;
    }
};

void OptDataMap()
{
    std::map<DataInfo, std::string, CmpKey> mapDebug;
    mapDebug.insert({{4, 'h', "abc"}, "abc"});
    mapDebug.insert({{2, 'd', "efg"}, "abddc"});
    mapDebug.insert({{1, 'f', "lmn"}, "afsdfdsbc"});
    mapDebug.insert({{2, 'c', "hik"}, "haha"});
    mapDebug.insert({{4, 'h', "def"}, "haha"});
    for (auto curKV : mapDebug) {
        std::cout << "{" << curKV.first.numKey << ", "
                  << curKV.first.charKey << ", "
                  << curKV.first.strKey << "}, "
                  << curKV.second << std::endl;
    }
}

int main() {
    OptDataMap();
}

/* 输出如下:
 * {1, f, lmn}, afsdfdsbc
 * {2, d, efg}, abddc
 * {2, c, hik}, haha
 * {4, h, abc}, abc
 * {4, h, def}, haha
 */

1.3 std::map 自定义排序续集

利用自定义数据结构的重载,完成排序,代码如下:


struct DataInfo {
    int numKey;
    char charKey;
    std::string strKey;

    DataInfo(int a, char b, std::string c) : 
        numKey(a), charKey(b), strKey(c) {};

    bool operator< (const DataInfo &input) const
    {
        if (this->numKey == input.numKey) {
            if (this->charKey == input.charKey) {
                return this->strKey < input.strKey;  // strKey 升序;
            }
            return this->charKey > input.charKey;  // charKey 降序;
        }
        return this->numKey < input.numKey;  // numKey 升序;
    }
};

void OptDataMap()
{
    std::map<DataInfo, std::string> mapDebug;
    mapDebug.insert({{4, 'h', "abc"}, "abc"});
    mapDebug.insert({{2, 'd', "efg"}, "abddc"});
    mapDebug.insert({{1, 'f', "lmn"}, "afsdfdsbc"});
    mapDebug.insert({{2, 'c', "hik"}, "haha"});
    mapDebug.insert({{4, 'h', "def"}, "haha"});
    for (auto curKV : mapDebug) {
        std::cout << "{" << curKV.first.numKey << ", "
                  << curKV.first.charKey << ", "
                  << curKV.first.strKey << "}, " << curKV.second << std::endl;
    }
}

/* 输出如下:
 * {1, f, lmn}, afsdfdsbc
 * {2, d, efg}, abddc
 * {2, c, hik}, haha
 * {4, h, abc}, abc
 * {4, h, def}, haha
 */

二、 std::map 的初始化及数据修改

  • 直接赋值,没啥介绍的,常规且基本操作,一笔带过
void MapInit()
{
    std::map<int, std::string> mapDebug;
    mapDebug[4] = "it`s 4";
    mapDebug[2] = "it`s 2";
    for (auto kv : mapDebug) {
        std::cout << "k:" << kv.first 
                  << ", v:" << kv.second
                  << std::endl;
    }
}

// k:2, v:it`s 2
// k:4, v:it`s 4
  • insert 方法赋值,cppreference.com 中的介绍是 insert 可以插入元素或节点(C++17 起)。这里分别实验一下
{
    using namespace std::string_literals; // 支持 "xxx"s 字面量
    std::map<std::string, float> heights;

    // 第一次插入(成功)
    const auto [it1, success1] = heights.insert({"Hinata"s, 162.8});
    std::cout << "Inserted? " << success1 // 输出 1(true)
              << "; Inserted at " << std::distance(heights.begin(), it1)
              << std::endl;

    // 第二次插入相同键(失败)
    const auto [it2, success2] = heights.insert({"Hinata"s, 165.0});
    std::cout << "Inserted? " << success2 // 输出 0(false)
              << "; Inserted at " << std::distance(heights.begin(), it2)
              << std::endl;

    // 第三次插入相同键(失败)
    const auto [it3, success3] = heights.insert({"Hinata3"s, 165.0});
    std::cout << "Inserted? " << success3 // 输出 1(true)
              << "; Inserted at " << std::distance(heights.begin(), it3)
              << std::endl;
// 输出如下:
// Inserted? 1; Inserted at 0
// Inserted? 0; Inserted at 0
// Inserted? 1; Inserted at 1
}

大概查了一下,insert 插入节点,并不怎么实用,这里一笔带过

#include <map>

int main() {
    std::map<int, std::string> m1, m2;
    m1[1] = "apple";

    // 提取节点(不释放内存)
    auto node = m1.extract(1);

    // 插入节点到另一个 map
    if (!node.empty()) {
        m2.insert(std::move(node));  // 无需重新分配内存
    }

    // 此时 m1 为空,m2 包含 {1, "apple"}
    return 0;
}

insert 从范围插入。从范围插入,必须是两个相同类型的 map,代码如下:

std::map<std::string, float> heights;    
std::map<std::string, float> heights2;
heights.insert({"Hinata"s, 162.8});
heights.insert({"Kageyama", 180.6});
 
// 从范围插入
heights2.insert(std::begin(heights), std::end(heights));

insert 还可以从列表插入,提供一个 initializer_list,一起插入,代码如下:

std::map<std::string, float> heights2;
heights2.insert({{"Kozume"s, 169.2}, {"Kuroo", 187.7}});
  •  使用 emplace 插入数据,代码如下:
#include <iostream>
#include <string>
#include <utility>
#include <map>
 
int main()
{
    std::map<std::string, std::string> m;
 
    // 使用 pair 的移动构造函数
    auto [it, result] = m.emplace(std::make_pair(std::string("a"), std::string("a")));
    std::cout << std::distance(m.begin(), it) << std::endl;  // 输出 0
    std::cout << result << std::endl;  //  输出 1,表示成功
 
    // 使用 pair 的转换移动构造函数
    m.emplace(std::make_pair("b", "abcd"));
 
    // 使用 pair 的模板构造函数
    m.emplace("d", "ddd");
 
    // 带有重复键的 emplace 没有效果
    auto [it1, result1] = m.emplace("d", "DDD");
    std::cout << std::distance(m.begin(), it1) << std::endl;
    // 输出 2,其实表示的是上面插入 d,ddd 的结果,就是 key=d 的下标
    std::cout << result1 << std::endl;  // 输出 0 ,表示插入失败
 
    for (const auto& p : m)
        std::cout << p.first << " => " << p.second << '\n';
}

/*
a => a
b => abcd
d => ddd
*/
  • 使用 try_emplace 插入数据。C++17 推荐使用 try_emplace 方式插入数据

  •  使用 insert_or_assign 方式插入数据。插入元素,或若键已存在则赋值给当前元素。这个使用与一定场景,如果已经有数据,可以覆盖原来的值
#include <iostream>
#include <string>
#include <map>
 
void print_node(const auto& node)
{
    std::cout << '[' << node.first << "] = " << node.second << '\n';
}
 
void print_result(auto const& pair)
{
    std::cout << (pair.second ? "inserted: " : "assigned: ");
    print_node(*pair.first);
}
 
int main()
{
    std::map<std::string, std::string> myMap;
 
    print_result(myMap.insert_or_assign("a", "apple"));
    print_result(myMap.insert_or_assign("b", "banana"));
    print_result(myMap.insert_or_assign("c", "cherry"));
    print_result(myMap.insert_or_assign("c", "clementine"));
 
    for (const auto& node : myMap)
        print_node(node);
}
/*
inserted: [a] = apple
inserted: [b] = banana
inserted: [c] = cherry
assigned: [c] = clementine
[a] = apple
[b] = banana
[c] = clementine
*/

extrace 提取容器中的节点,提取之后,map 中就没有这个节点数据了。

#include <algorithm>
#include <iostream>
#include <string_view>
#include <map>
 
void print(std::string_view comment, const auto& data)
{
    std::cout << comment;
    for (auto [k, v] : data)
        std::cout << ' ' << k << '(' << v << ')';
 
    std::cout << '\n';
}
 
int main()
{
    std::map<int, char> cont{{1, 'a'}, {2, 'b'}, {3, 'c'}};
 
    print("Start:", cont);
 
    // 提取节点句柄并改变键
    auto nh = cont.extract(1);
    nh.key() = 4;  //  注意这个用法,比较实用
 
    print("After extract and before insert:", cont);
 
    // 将节点句柄插回去
    cont.insert(std::move(nh));
 
    print("End:", cont);
}

/*
输出:
Start: 1(a) 2(b) 3(c)
After extract and before insert: 2(b) 3(c)
End: 2(b) 3(c) 4(a)
*/
  •  erase() 擦除元素。关键点是注意 for 循环和 while 循环时候的自增运算
#include <map>
#include <iostream>
 
int main()
{
    std::map<int, std::string> c =
    {
        {1, "one" }, {2, "two" }, {3, "three"},
        {4, "four"}, {5, "five"}, {6, "six"  }
    };
 
    // 从 c 移除所有奇数
    for (auto it = c.begin(); it != c.end(); )
    {  //  注意 for 循环的写法,第三个表达式不写
        if (it->first % 2 != 0)
            it = c.erase(it);  // 调用了 erase 的话,就不++
        else
            ++it;  // 没调用 erase 的时候,for 循环 ++
    }
 
    for (auto& p : c)
        std::cout << p.second << ' ';
    std::cout << '\n';
}
  • merge() 函数,假如当前有两个 map 对象,m1 和 m2,将 m2 merge 到 m1 里面,merge 会尝试从 m2 中提取每个元素,然后插入到 m1 中。插入到 m1 的时候,如果 m1 中已经有等价的元素,则不会从 m2 中提取元素。如果 m1 中没有等价的元素,则会提取并插入到 m1 中,需要注意的是,从 m2 中提取元素会修改 m2 的数据,m2 中不再有提取出来的元素。代码:
void MapMergeDebug() {
    std::map<int, std::string> ma{{1, "apple"}, {5, "pear"}, {10, "banana"}};
    std::map<int, std::string> mb{{2, "zorro"}, {4, "batman"}, {5, "X"}, {8, "alpaca"}};

    ma.merge(mb);

    std::cout << "ma.size(): " << ma.size() << '\n';
    std::cout << "mb.size(): " << mb.size() << '\n';
    std::cout << "ma: ";
    for (auto const& kv : ma)
        std::cout << kv.first << "," << kv.second << "; ";
    std::cout << std::endl;
    std::cout << "mb: ";
    for (auto const& kv : mb)
        std::cout << kv.first << "," << kv.second << "; ";
    std::cout << std::endl;
}
/* 输出:
ma.size(): 6
mb.size(): 1
ma: 1,apple; 2,zorro; 4,batman; 5,pear; 8,alpaca; 10,banana; 
mb: 5,X;
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值