std::multimap

本文详细介绍了C++标准库中的关联容器std::multimap,包括其构造、元素访问、迭代器操作、容量管理、元素修改、查找功能,并通过实例演示了各种成员函数的使用。此外,还提到了非成员函数,如比较运算符在多映射中的应用。
摘要由CSDN通过智能技术生成

std::multimap

介绍

成员函数

非成员函数

介绍

// multimap 模板定义
template<class Key, class T, class Compare = std::less<Key>,
    class Allocator = std::allocator<std::pair<const Key, T>>> class multimap;
namespace pmr {
    template< class Key, class T, class Compare = std::less<Key> >
    using multimap = std::multimap<Key, T, Compare,
        std::pmr::polymorphic_allocator<std::pair<const Key, T>>>;
}
  • std::multimap 介绍摘选自 cppreference.com 中文网 std::multimap 介绍
  • multimap 是关联容器,含有键值对的已排序列表,同时容许多个元素拥有同一键,按照应用到键的比较函数 Compare 排序
  • 搜索、插入和移除操作拥有对数复杂度
  • 拥有等价键的键值对的顺序就是插入顺序,且不会更改
  • 标准库使用比较 (Compare) 概念时用等价关系确定唯一性
  • 不精确地说,如果两个对象 a 与 b 相互不比较小于对方:!comp(a, b) && !comp(b, a),那么认为它们等价

成员函数

构造析构
#include <QCoreApplication>
#include <algorithm>
#include <iostream>
#include <map>
#include <string>

auto Print(const std::string &msg, const std::multimap<int, int> &lst) {
  std::cout << msg << " : ";
  for (const auto &pa : lst) {
    std::cout << pa.first << " - " << pa.second << "\t";
  }
  std::cout << "\n";
}

int main(int argc, char *argv[]) {
  QCoreApplication a(argc, argv);

  using map_int = std::multimap<int, int>;
  map_int m1; //默认构造
  Print("m1", m1);

  std::less<int> cmp;
  std::allocator<std::pair<int, int>> alloc;
  map_int m2(cmp, alloc); //设置比较器和分配器
  Print("m2", m2);
  map_int m3(alloc);
  Print("m3", m3);

  map_int m4{{1, 1}, {1, 2}, {2, 1}, {2, 2}};
  Print("m4", m4);

  map_int m5(m4.begin(), m4.end()); //迭代器初始化
  Print("m5", m5);

  map_int m6(m5); //拷贝构造初始化
  Print("m6", m6);
  map_int m7(m6, alloc);
  Print("m7", m7);

  map_int m8(std::move(m7)); //移动构造初始化
  Print("m8", m8);
  map_int m9(std::move(m8), alloc);
  Print("m9", m9);

  map_int m10{{1, 1}, {1, 2}, {2, 1}, {2, 2}}; //初始化列表
  Print("m10", m10);

  map_int m11 = m10; // 赋值运算符
  Print("m11", m11);

  // 默认析构
  return 0; // a.exec();
}

输出结果:
m1 :
m2 :
m3 :
m4 : 1 - 1 1 - 2 2 - 1 2 - 2
m5 : 1 - 1 1 - 2 2 - 1 2 - 2
m6 : 1 - 1 1 - 2 2 - 1 2 - 2
m7 : 1 - 1 1 - 2 2 - 1 2 - 2
m8 : 1 - 1 1 - 2 2 - 1 2 - 2
m9 : 1 - 1 1 - 2 2 - 1 2 - 2
m10 : 1 - 1 1 - 2 2 - 1 2 - 2
m11 : 1 - 1 1 - 2 2 - 1 2 - 2

元素访问
auto Print(const std::string &msg, const std::multimap<int, int> &lst) {
  std::cout << msg << " : ";
  for (const auto &pa : lst) {
    std::cout << pa.first << " - " << pa.second << "\t";
  }
  std::cout << "\n";
}

int main(int argc, char *argv[]) {
  QCoreApplication a(argc, argv);

  using map_int = std::multimap<int, int>;
  map_int m1{{1, 1}, {1, 2}, {2, 1}, {2, 2}};
  Print("m1", m1);

  std::cout << "m1 : ";
  for (auto iter = m1.begin(); iter != m1.end(); ++iter) { // 打印 key 的元素
    if (iter->first == 1)
      std::cout << iter->first << " - " << iter->second << "\t";
  }
  std::cout << std::endl;
  return 0; // a.exec();
}

输出结果:
m1 : 1 - 1 1 - 2 2 - 1 2 - 2
m1 : 1 - 1 1 - 2

迭代器
auto Print(const std::string &msg, const std::multimap<int, int> &lst) {
  std::cout << msg << " : ";
  for (const auto &pa : lst) {
    std::cout << pa.first << " - " << pa.second << "\t";
  }
  std::cout << "\n";
}

int main(int argc, char *argv[]) {
  QCoreApplication a(argc, argv);

  using map_int = std::multimap<int, int>;
  map_int m1{{1, 1}, {1, 2}, {2, 1}, {2, 2}};
  Print("m1", m1);

  map_int::iterator iter = m1.begin(); //返回指向起始的迭代器
  std::cout << "m1 : ";
  for (; iter != m1.end(); ++iter) {
    std::cout << iter->first << " - " << iter->second << "\t";
  }
  std::cout << "\n";

  map_int::const_iterator citer = m1.begin(); //返回指向起始的迭代器
  std::cout << "m1 : ";
  for (; citer != m1.end(); ++citer) {
    std::cout << citer->first << " - " << citer->second << "\t";
  }
  std::cout << "\n";

  map_int::reverse_iterator riter = m1.rbegin(); //返回指向起始的逆向迭代器
  std::cout << "m1 : ";
  for (; riter != m1.rend(); ++riter) {
    std::cout << riter->first << " - " << riter->second << "\t";
  }
  std::cout << "\n";

  map_int::const_reverse_iterator rciter =
      m1.crbegin(); //返回指向起始的逆向迭代器
  std::cout << "m1 : ";
  for (; rciter != m1.crend(); ++rciter) {
    std::cout << rciter->first << " - " << rciter->second << "\t";
  }
  std::cout << "\n";

  return 0; // a.exec();
}

输出结果:
m1 : 1 - 1 1 - 2 2 - 1 2 - 2
m1 : 1 - 1 1 - 2 2 - 1 2 - 2
m1 : 1 - 1 1 - 2 2 - 1 2 - 2
m1 : 2 - 2 2 - 1 1 - 2 1 - 1
m1 : 2 - 2 2 - 1 1 - 2 1 - 1

容量
auto Print(const std::string &msg, const std::multimap<int, int> &lst) {
  std::cout << msg << " : ";
  for (const auto &pa : lst) {
    std::cout << pa.first << " - " << pa.second << "\t";
  }
  std::cout << "\n";
}

int main(int argc, char *argv[]) {
  QCoreApplication a(argc, argv);

  using map_int = std::multimap<int, int>;
  map_int m1{{1, 1}, {1, 2}, {2, 1}, {2, 2}};
  Print("m1", m1);

  //检查容器是否为空
  std::cout << std::boolalpha << "m1.empty() : " << m1.empty() << std::endl;

  std::cout << "m1.size() : " << m1.size() << std::endl; //返回容纳的元素数
  std::cout << "m1.max_size() : " << m1.max_size()
            << std::endl; //返回可容纳的最大元素数,和平台有关

  return 0; // a.exec();
}

输出结果:
m1 : 1 - 1 1 - 2 2 - 1 2 - 2
m1.empty() : false
m1.size() : 4
m1.max_size() : 461168601842738790

修改器
auto Print(const std::string &msg, const std::multimap<int, int> &lst) {
  std::cout << msg << " : ";
  for (const auto &pa : lst) {
    std::cout << pa.first << " - " << pa.second << "\t";
  }
  std::cout << "\n";
}

int main(int argc, char *argv[]) {
  QCoreApplication a(argc, argv);

  using map_int = std::multimap<int, int>;
  map_int m1{{1, 1}, {1, 2}, {2, 1}, {2, 2}};
  Print("m1", m1);

  m1.clear(); //  清除内容
  Print("m1", m1);
  m1.insert(std::pair<int, int>(1, 10)); //插入元素或结点
  m1.insert({1, 10});
  Print("m1", m1);
  m1.emplace(3, 30); //原位构造元素
  m1.emplace(3, 30);
  Print("m1", m1);

  m1.emplace_hint(m1.end(), 5, 50); //使用提示原位构造元素
  m1.emplace_hint(m1.end(), 6, 60);
  m1.emplace_hint(m1.end(), 7, 70);
  Print("m1", m1);

  m1.erase(m1.begin()); //擦除元素
  Print("m1", m1);

  map_int m2;
  m2.swap(m1);
  Print("m1", m1);
  Print("m2", m2);

  return 0; // a.exec();
}

输出结果:
m1 : 1 - 1 1 - 2 2 - 1 2 - 2
m1 :
m1 : 1 - 10 1 - 10
m1 : 1 - 10 1 - 10 3 - 30 3 - 30
m1 : 1 - 10 1 - 10 3 - 30 3 - 30 5 - 50 6 - 60 7 - 70
m1 : 1 - 10 3 - 30 3 - 30 5 - 50 6 - 60 7 - 70
m1 :
m2 : 1 - 10 3 - 30 3 - 30 5 - 50 6 - 60 7 - 70

查找
auto Print(const std::string &msg, const std::multimap<int, int> &lst) {
  std::cout << msg << " : ";
  for (const auto &pa : lst) {
    std::cout << pa.first << " - " << pa.second << "\t";
  }
  std::cout << "\n";
}

int main(int argc, char *argv[]) {
  QCoreApplication a(argc, argv);

  using map_int = std::multimap<int, int>;
  map_int m1{{1, 1}, {1, 2}, {2, 1}, {2, 2}};
  Print("m1", m1);

  std::cout << "m1.count(1) : " << m1.count(1)
            << std::endl; //返回匹配特定键的元素数量

  //寻找键等于 key 的的元素,若容器中有数个拥有键 key 的元素,则可能返回任意一者
  auto it = m1.find(2); //寻找带有特定键的元素
  if (it == m1.end()) {
    std::cout << "m1.find(2) : m1.end()" << std::endl;
  } else {
    std::cout << "m1.find(2) : " << it->first << " - " << it->second
              << std::endl;
  }

  //返回容器中所有拥有给定关键的元素范围。范围以二个迭代器定义,一个指向首个不小于
  // key 的元素,另一个指向首个大于 key 的元素。首个迭代器可以换用 lower_bound()
  //获得,而第二迭代器可换用 upper_bound() 获得
  std::pair<map_int::iterator, map_int::iterator> pa =
      m1.equal_range(1); //返回匹配特定键的元素范围

  std::cout << pa.first->first << " - " << pa.first->second << std::endl;
  std::cout << pa.second->first << " - " << pa.second->second << std::endl;

  map_int::iterator lit =
      m1.lower_bound(2); //返回指向首个不小于给定键的元素的迭代器
  std::cout << "m1.lower_bound(2) : " << lit->first << " - " << lit->second
            << std::endl;

  lit = m1.upper_bound(1); //返回指向首个大于给定键的元素的迭代器
  std::cout << "m1.upper_bound(1) : " << lit->first << " - " << lit->second
            << std::endl;

  return 0; // a.exec();
}

输出结果:
m1 : 1 - 1 1 - 2 2 - 1 2 - 2
m1.count(1) : 2
m1.find(2) : 2 - 1
1 - 1
2 - 1
m1.lower_bound(2) : 2 - 1
m1.upper_bound(1) : 2 - 1

非成员函数

auto Print(const std::string &msg, const std::multimap<int, int> &lst) {
  std::cout << msg << " : ";
  for (const auto &pa : lst) {
    std::cout << pa.first << " - " << pa.second << "\t";
  }
  std::cout << "\n";
}

int main(int argc, char *argv[]) {
  QCoreApplication a(argc, argv);

  using map_int = std::multimap<int, int>;
  map_int m1{{1, 1}, {1, 2}, {2, 1}, {2, 2}};
  map_int m2{{1, 3}, {1, 2}, {2, 5}, {2, 2}};
  Print("m1", m1);
  Print("m2", m2);

  std::cout.setf(std::ios::boolalpha);
  std::cout << "m1 == m2 : " << (m1 == m2) << std::endl;
  std::cout << "m1 != m2 : " << (m1 != m2) << std::endl;
  std::cout << "m1 > m2 : " << (m1 > m2) << std::endl;
  std::cout << "m1 >= m2 : " << (m1 >= m2) << std::endl;
  std::cout << "m1 < m2 : " << (m1 < m2) << std::endl;
  std::cout << "m1 <= m2 : " << (m1 <= m2) << std::endl;
  std::cout.unsetf(std::ios::boolalpha);
  // c++20 废弃以上操作符重载,提供三路运算符 operator <=> ()

  std::swap(m1, m2);
  Print("m1", m1);
  Print("m2", m2);

  return 0; // a.exec();
}

输出结果:
m1 : 1 - 1 1 - 2 2 - 1 2 - 2
m2 : 1 - 3 1 - 2 2 - 5 2 - 2
m1 == m2 : false
m1 != m2 : true
m1 > m2 : false
m1 >= m2 : false
m1 < m2 : true
m1 <= m2 : true
m1 : 1 - 3 1 - 2 2 - 5 2 - 2
m2 : 1 - 1 1 - 2 2 - 1 2 - 2

起始

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值