当前位置: 首页 > news >正文

c++11 标准模板(STL)(std::multimap)(二)

定义于头文件 <map>
template<

    class Key,
    class T,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<std::pair<const Key, T> >

> class multimap;
(1)
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>>>;

}
(2)(C++17 起)

 

multimap 是关联容器,含有关键-值 pair 的已排序列表,同时容许多个入口拥有同一关键。按照应用到关键的比较函数 Compare 排序。搜索、插入和移除操作拥有对数复杂度。

拥有等价关键的关键-值 pair 的顺序就是插入顺序,且不会更改。(C++11 起)

凡在标准库使用比较 (Compare) 概念出,都用描述于比较 (Compare) 上的等价关系确定等价性。不精确地说,若二个对象 ab 互不小于对方: !comp(a, b) && !comp(b, a) ,则认为它们等价。

成员函数

构造函数

std::multimap<Key,T,Compare,Allocator>::multimap
multimap();

explicit multimap( const Compare& comp,

                   const Allocator& alloc = Allocator() );
(1)

explicit multimap( const Allocator& alloc );

(1)(C++11 起)
template< class InputIt >

multimap( InputIt first, InputIt last,
          const Compare& comp = Compare(),

          const Allocator& alloc = Allocator() );
(2)
template< class InputIt >

multimap( InputIt first, InputIt last,

          const Allocator& alloc );
(C++14 起)

multimap( const multimap& other );

(3)

multimap( const multimap& other, const Allocator& alloc );

(3)(C++11 起)

multimap( multimap&& other );

(4)(C++11 起)

multimap( multimap&& other, const Allocator& alloc );

(4)(C++11 起)
multimap( std::initializer_list<value_type> init,

          const Compare& comp = Compare(),

          const Allocator& alloc = Allocator() );
(5)(C++11 起)

multimap( std::initializer_list<value_type> init,
          const Allocator& );

(C++14 起)

 

从各种数据源构造新容器,可选地使用用户提供的分配器 alloc 或比较函数对象 comp

1) 构造空容器。

2) 构造容器,使之拥有范围 [first, last) 的内容。

3) 复制构造函数。构造容器,使之拥有 other 的内容副本。若不提供 alloc ,则通过调用 std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()) 获得分配器。

4) 移动构造函数。用移动语义构造容器,使之拥有 other 的内容。若不提供 alloc ,则从属于 other 的分配器移动构造分配器

5) 构造容器,使之拥有 initializer_list init 的内容。

参数

alloc-用于此容器所有内存分配的分配器
comp-用于所有关键比较的比较函数对象
first, last-复制元素来源的范围
other-要用作源以初始化容器元素的另一容器
init-用以初始化容器元素的 initializer_list
类型要求
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。
- Compare 必须满足比较 (Compare) 的要求。
- Allocator 必须满足分配器 (Allocator) 的要求。

复杂度

1) 常数。

2) N log(N) ,其中通常有 N = std::distance(first, last) ,若范围已为 value_comp() 所排序则与 N 成线性。

3) 与 other 的大小成线性。

4) 常数。若给定 alloc 且 alloc != other.get_allocator() 则为线性。

5) N log(N) ,其中通常有 N = init.size()) ,若 init 已按照 value_comp() 排序则与 N 成线性 。

异常

Allocator::allocate 的调用可能抛出。

注意

在容器移动构造(重载 (4) )后,指向 other 的引用及迭代器(除了尾迭代器)保持合法,但指代现于 *this 中的元素。当前标准由 [container.requirements.general]/12 中的总括陈述作出此保证,而 LWG 2321 正在考虑更严格的保证。

析构函数

std::multimap<Key,T,Compare,Allocator>::~multimap

~multimap();

销毁容器。调用元素的析构函数,然后解分配所用的存储。注意,若元素是指针,则不销毁所指向的对象。

复杂度

与容器大小成线性。

调用示例

#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <map>
#include <time.h>using namespace std;struct Cell
{int x;int y;Cell() = default;Cell(int a, int b): x(a), y(b) {}Cell &operator +=(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator +(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator *(const Cell &cell){x *= cell.x;y *= cell.y;return *this;}Cell &operator ++(){x += 1;y += 1;return *this;}bool operator <(const Cell &cell) const{if (x == cell.x){return y < cell.y;}else{return x < cell.x;}}bool operator >(const Cell &cell) const{if (x == cell.x){return y > cell.y;}else{return x > cell.x;}}bool operator ==(const Cell &cell) const{return x == cell.x && y == cell.y;}
};struct myCompare
{bool operator()(const int &a, const int &b){return a < b;}
};std::ostream &operator<<(std::ostream &os, const Cell &cell)
{os << "{" << cell.x << "," << cell.y << "}";return os;
}std::ostream &operator<<(std::ostream &os, const std::pair<const int, Cell> &pCell)
{os << pCell.first << "-" << pCell.second;return os;
}int main()
{std::cout << std::boolalpha;std::mt19937 g{std::random_device{}()};srand((unsigned)time(NULL));auto genKey = [](){return std::rand() % 10 + 100;};auto generate = [](){int n = std::rand() % 10 + 100;Cell cell{n, n};return cell;};//1) 构造空容器。std::multimap<int, Cell> multimap1;std::cout << "multimap1 is empty: " << multimap1.empty() << std::endl;for (size_t index = 0; index < 5; index++){multimap1.insert({genKey(), generate()});}std::cout << std::endl;//2) 构造容器,使之拥有范围 [first, last) 的内容。std::multimap<int, Cell> multimap2(multimap1.begin(), multimap1.end());std::cout << "multimap2:    ";std::copy(multimap2.begin(), multimap2.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));std::cout << std::endl;// 逆序std::multimap<int, Cell, std::greater<int>> multimap8(multimap1.begin(), multimap1.end());std::cout << "multimap8:    ";std::copy(multimap8.begin(), multimap8.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));std::cout << std::endl;std::cout << std::endl;//3) 复制构造函数。构造容器,使之拥有 other 的内容副本。std::multimap<int, Cell> multimap3(multimap2);std::cout << "multimap3:    ";std::copy(multimap3.begin(), multimap3.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));std::cout << std::endl;std::multimap<int, Cell, std::greater<int>> multimap9(multimap8);std::cout << "multimap9:    ";std::copy(multimap9.begin(), multimap9.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));std::cout << std::endl;std::cout << std::endl;//4) 移动构造函数。用移动语义构造容器,使之拥有 other 的内容。std::multimap<int, Cell> multimap4(std::move(multimap2));std::cout << "multimap4:    ";std::copy(multimap4.begin(), multimap4.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));std::cout << std::endl;std::cout << "multimap2(move) is empty: " << multimap2.empty() << std::endl;std::multimap<int, Cell, std::greater<int>> multimap10(std::move(multimap8));std::cout << "multimap10:   ";std::copy(multimap10.begin(), multimap10.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));std::cout << std::endl;std::cout << "multimap8(move) is empty: " << multimap10.empty() << std::endl;std::cout << std::endl;//5) 构造容器,使之拥有 initializer_list init 的内容。std::multimap<int, Cell> multimap5({{genKey(), generate()}, {genKey(), generate()},{genKey(), generate()}, {genKey(), generate()}, {genKey(), generate()}});std::cout << "multimap5:    ";std::copy(multimap5.begin(), multimap5.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));std::cout << std::endl;std::multimap<int, Cell, std::greater<int>> multimap11({{genKey(), generate()}, {genKey(), generate()},{genKey(), generate()}, {genKey(), generate()}, {genKey(), generate()}});std::cout << "multimap11:   ";std::copy(multimap11.begin(), multimap11.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));std::cout << std::endl;std::cout << std::endl;//使用系统key比较函数std::multimap<int, Cell, std::greater<int>> multimap6({{genKey(), generate()}, {genKey(), generate()},{genKey(), generate()}, {genKey(), generate()}, {genKey(), generate()}});std::cout << "multimap6:    ";std::copy(multimap6.begin(), multimap6.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));std::cout << std::endl;std::cout << std::endl;//使用自定义key比较函数std::multimap<int, Cell, myCompare> multimap7({{genKey(), generate()}, {genKey(), generate()},{genKey(), generate()}, {genKey(), generate()}, {genKey(), generate()}});std::cout << "multimap7:    ";std::copy(multimap7.begin(), multimap7.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));std::cout << std::endl;return 0;
}

 

http://www.lryc.cn/news/1400.html

相关文章:

  • 【数据结构】二叉排序树——平衡二叉树的调整
  • 03- pandas 数据库可视化 (数据库)
  • 第三方电容笔怎么样?开学适合买的电容笔
  • Java学习-IO流-字节输出流
  • linux性能分析 性能之巅学习笔记和内容摘录
  • 机器学习笔记之生成模型综述(三)生成模型的表示、推断、学习任务
  • 第八章 Flink集成Iceberg的DataStreamAPI、TableSQLAPI详解
  • PyTorch学习笔记:nn.Sigmoid——Sigmoid激活函数
  • 个人学习系列 - 解决拦截器操作请求参数后台无法获取
  • 【编程基础之Python】2、安装Python环境
  • Java开发 - 问君能有几多愁,Spring Boot瞅一瞅。
  • Office Server Document Converter Lib SDK Crack
  • Cubox是什么应用?如何将Cubox同步至Notion、语雀、在线文档中
  • 计算机网络-传输层
  • HTML-CSS-js教程
  • 【Nacos】Nacos配置中心客户端启动源码分析
  • 中国特色地流程管理系统,天翎让流程审批更简单
  • Python算法:DFS排列与组合算法(手写模板)
  • 拿来就用的Java海报生成器ImageCombiner(一)
  • 【C++】类和对象(二)
  • UDP协议
  • IT人的晋升之路——关于人际交往能力的培养
  • Docker进阶 - 8. docker network 网络模式之 container
  • 2年功能测试月薪9.5K,100多天自学自动化,跳槽涨薪4k后我的路还很长...
  • “数字孪生”:为什么要仿真嵌入式系统?
  • Java基础知识总结(上)
  • MySQL 2:MySQL约束
  • C4--Vivado添加列表中不存在的FLash器件2023-02-10
  • php代码审计
  • 接口测试入门,如何划分接口文档