std::map

std::map

介绍

成员函数

  • 构造析构
  • 元素访问
  • 迭代器
  • 容量
  • 修改器
  • 查找

非成员函数

介绍

// map 模板定义
template, class Allocator = std::allocator>> class map;	
namespace pmr {
    template< class Key, class T, class Compare = std::less >
    using map = std::map>>
}
  • std::map 介绍摘选自 cppreference.com 中文网 std::map 介绍
  • std::map 是有序键值对容器,它的元素的键是唯一的
  • 用比较函数 Compare 排序键、搜索、移除和插入操作拥有对数复杂度
  • map 通常实现为红黑树
  • 标准库使用比较 (Compare) 概念时用等价关系确定唯一性
  • 不精确地说,如果两个对象 a 与 b 相互不比较小于对方:!comp(a, b) && !comp(b, a),那么认为它们等价

成员函数

构造析构
#include 
#include 
#include 
#include 
#include 

auto Print(const std::string &msg, const std::map &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::map;
  map_int m1; //默认构造
  Print("m1", m1);

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

  map_int m3(alloc); //设置分配器
  Print("m3", m3);

  map_int m4{{2, 2}, {3, 3}, {4, 4}};
  Print("m4", m4);
  map_int m5(m4.begin(), m4.end()); //迭代器初始化
  Print("m5", m5);

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

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

  map_int m8{{2, 2}, {3, 3}, {4, 4}}; //初始化列表
  Print("m8", m8);

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

输出结果:
m1 :
m2 :
m3 :
m4 : 2 - 2 3 - 3 4 - 4
m5 : 2 - 2 3 - 3 4 - 4
m6 : 2 - 2 3 - 3 4 - 4
m7 : 2 - 2 3 - 3 4 - 4
m8 : 2 - 2 3 - 3 4 - 4

元素访问
auto Print(const std::string &msg, const std::map &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::map;
  map_int m1{{100, 10}, {200, 20}, {300, 30}, {400, 40}, {500, 50}};
  Print("m1", m1);

  std::cout << "m1.at(100) : " << m1.at(100) << std::endl;
  std::cout << "m1.at(300) : " << m1.at(300) << std::endl;
  std::cout << "m1.at(500) : " << m1.at(500) << std::endl;

  std::cout << "m1[100] : " << m1[100] << std::endl;
  std::cout << "m1[300] : " << m1[300] << std::endl;
  std::cout << "m1[500] : " << m1[500] << std::endl;

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

输出结果:
m1 : 100 - 10 200 - 20 300 - 30 400 - 40 500 - 50
m1.at(100) : 10
m1.at(300) : 30
m1.at(500) : 50
m1[100] : 10
m1[300] : 30
m1[500] : 50

迭代器
auto Print(const std::string &msg, const std::map &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::map;
  map_int m1{{100, 10}, {200, 20}, {300, 30}, {400, 40}, {500, 50}};
  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 rbiter = m1.rbegin(); //返回指向起始的逆向迭代器
  std::cout << "m1 : ";
  for (; rbiter != m1.rend(); ++rbiter) {
    std::cout << rbiter->first << " - " << rbiter->second << "\t";
  }
  std::cout << "\n";

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

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

输出结果:
m1 : 100 - 10 200 - 20 300 - 30 400 - 40 500 - 50
m1 : 100 - 10 200 - 20 300 - 30 400 - 40 500 - 50
m1 : 100 - 10 200 - 20 300 - 30 400 - 40 500 - 50
m1 : 500 - 50 400 - 40 300 - 30 200 - 20 100 - 10
m1 : 500 - 50 400 - 40 300 - 30 200 - 20 100 - 10

容量
auto Print(const std::string &msg, const std::map &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::map;
  map_int m1{{100, 10}, {200, 20}, {300, 30}, {400, 40}, {500, 50}};
  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 : 100 - 10 200 - 20 300 - 30 400 - 40 500 - 50
m1.empty() : false
m1.size() : 5
m1.max_size() : 461168601842738790

修改器
auto Print(const std::string &msg, const std::map &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::map;
  map_int m1{{100, 10}, {200, 20}, {300, 30}, {400, 40}, {500, 50}};
  Print("m1", m1);

  m1.clear(); //  清除内容
  Print("m1", m1);
  m1.insert(std::pair(1, 10)); //插入元素或结点
  m1.insert({2, 20});
  Print("m1", m1);
  m1.emplace(3, 30); //原位构造元素
  m1.emplace(4, 40);
  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);
  
  return 0; // a.exec();
}

输出结果:
m1 : 100 - 10 200 - 20 300 - 30 400 - 40 500 - 50
m1 :
m1 : 1 - 10 2 - 20
m1 : 1 - 10 2 - 20 3 - 30 4 - 40
m1 : 1 - 10 2 - 20 3 - 30 4 - 40 5 - 50 6 - 60 7 - 70
m1 : 2 - 20 3 - 30 4 - 40 5 - 50 6 - 60 7 - 70

查找
auto Print(const std::string &msg, const std::map &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::map;
  map_int m1{{100, 10}, {200, 20}, {300, 30}, {400, 40}, {500, 50}};
  Print("m1", m1);

  std::cout << "m1.count() : " << m1.count(300)
            << std::endl; //返回匹配特定键的元素数量
  auto it = m1.find(200); //寻找带有特定键的元素
  if (it == m1.end()) {
    std::cout << "m1.find(200) : m1.end()" << std::endl;
  } else {
    std::cout << "m1.find(200) : " << it->first << " - " << it->second
              << std::endl;
  }

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

  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(300); //返回指向首个不小于给定键的元素的迭代器
  std::cout << "m1.lower_bound(300) : " << lit->first << " - " << lit->second
            << std::endl;

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

输出结果:
m1 : 100 - 10 200 - 20 300 - 30 400 - 40 500 - 50
m1.count() : 1
m1.find(200) : 200 - 20
100 - 10
200 - 20
m1.lower_bound(300) : 300 - 30
m1.upper_bound(100) : 200 - 20

非成员函数

auto Print(const std::string &msg, const std::map &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::map;
  map_int m1{{100, 10}, {200, 20}, {300, 30}, {400, 40}, {500, 50}};
  map_int m2{{100, 1}, {200, 2}, {300, 3}, {400, 4}, {500, 5}};
  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 : 100 - 10 200 - 20 300 - 30 400 - 40 500 - 50
m2 : 100 - 1 200 - 2 300 - 3 400 - 4 500 - 5
m1 == m2 : false
m1 != m2 : true
m1 > m2 : true
m1 >= m2 : true
m1 < m2 : false
m1 <= m2 : false
m1 : 100 - 1 200 - 2 300 - 3 400 - 4 500 - 5
m2 : 100 - 10 200 - 20 300 - 30 400 - 40 500 - 50

起始

你可能感兴趣的:(c++,stl,c++)