当前位置: 首页 > 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>::operator=

multimap& operator=( const multimap& other );

(1)

multimap& operator=( multimap&& other );

(2)(C++11 起)
(C++17 前)

multimap& operator=( multimap&& other ) noexcept(/* see below */);

(C++17 起)

multimap& operator=( std::initializer_list<value_type> ilist );

(3)(C++11 起)

替换容器内容。

1) 复制赋值运算符。以 other 的副本替换内容。若 std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value 为 true ,则以源分配器的副本替换目标分配器。若源分配器与目标分配器不比较相等,则用目标( *this )分配器销毁内存,然后在复制元素前用 other 的分配器分配。 (C++11 起).、

2) 移动赋值运算符。用移动语义以 other 的内容替换内容(即从 other 移动 other 中的数据到此容器)。之后 other 在合法但未指定的状态。若 std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value 为 true ,则用源分配器的副本替换目标分配器。若它为 false 且源与目标分配器不比较相等,则目标不能取走源内存的所有权,而必须单独移动赋值逐个元素,用自己的分配器按需分配额外的内存。任何情况下,原先在 *this 中的元素要么被销毁,要么以逐元素移动赋值替换。

3) 以 initializer_list ilist 所标识者替换内容。

参数

other-用作数据源的另一容器
ilist-用作数据源的 initializer_list

返回值

*this

复杂度

1) 与 *thisother 的大小成线性。

2) 与 *this 的大小成线性,除非分配器不比较相等且不传播,该情况下与 *thisother 的大小成线性。

3) 通常为 O(NlogN) ,其中 N 为 size() + ilist.size() 。若 ilist 相对于 value_comp() 已排序则为线性。

异常

2)noexcept 规定:  

noexcept(std::allocator_traits<Allocator>::is_always_equal::value
&& std::is_nothrow_move_assignable<Compare>::value)

(C++17 起)

注意

容器移动赋值(重载 (2) )后,除非不兼容的分配器强制逐元素赋值,否则指向 other 的引用、指针和迭代器(除了尾迭代器)都保持合法,不过指代的元素现在在 *this 中。当前标准通过 §23.2.1[container.requirements.general]/12 中的总括陈述保证这点,而 LWG 2321 下正在考虑更直接的保证。

返回相关的分配器

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

allocator_type get_allocator() const;

返回与容器关联的分配器。

参数

(无)

返回值

关联的分配器。

复杂度

常数。

 

调用示例

#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;};std::multimap<int, Cell> multimap1;for (size_t index = 0; index < 5; index++){multimap1.insert({genKey(), generate()});}std::cout << "multimap1:    ";std::copy(multimap1.begin(), multimap1.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));std::cout << std::endl;//1) 复制赋值运算符。以 other 的副本替换内容。std::multimap<int, Cell> multimap2 = multimap1;std::cout << "multimap2:    ";std::copy(multimap2.begin(), multimap2.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));std::cout << std::endl;//2) 移动赋值运算符。用移动语义以 other 的内容替换内容std::multimap<int, Cell> multimap3 = std::move(multimap1);std::cout << "multimap3:    ";std::copy(multimap3.begin(), multimap3.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));std::cout << std::endl;//3) 以 initializer_list ilist 所标识者替换内容。std::multimap<int, Cell, std::greater<int>> multimap4({{genKey(), generate()}, {genKey(), generate()},{genKey(), generate()}, {genKey(), generate()}, {genKey(), generate()}});std::cout << "multimap4:    ";std::copy(multimap4.begin(), multimap4.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));std::cout << std::endl;return 0;
}

输出

 

 

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

相关文章:

  • 【报复性赚钱】2023年5大风口行业
  • 单目相机、双目相机和RGB-D相机学习笔记(一些视频和博文网址)
  • word和wps添加mathtype选项卡
  • 获取成员userID
  • DOM编程-显示网页时钟
  • 浅谈保护数据的加密策略
  • Java中String,StringBuffer和StringBuilder
  • 华为认证常见技术问答整理:什么是Datacom认证?
  • Read book Netty in action (Chapter II) (Netty Introduction)
  • python--route
  • java面试中被问到项目中的难点,怎么回答
  • 【速通版】吴恩达机器学习笔记Part1
  • 面试(九)小米C++开发一面 21.11.02
  • 儿童书写台灯哪个牌子比较好?2023儿童护眼台灯分享
  • 市场调研计划书如何写?
  • python网络爬虫—快速入门(理论+实战)(七)
  • 机器学习笔记——Chapter 1 – The Machine Learning landscape
  • skimage.feature--corner_harris、hog、local_binary_pattern说明
  • 致敬白衣天使,学习Python读取
  • JVM - 认识JVM规范
  • 文献阅读笔记 # CodeBERT: A Pre-Trained Model for Programming and Natural Languages
  • openHarmony的UI开发
  • 【JavaSE】深入HashMap
  • 华为机试题:HJ62 查找输入整数二进制中1的个数(python)
  • 代码随想录训练营一刷总结|
  • CSS中的几种尺寸单位
  • 运维必会:ansible剧本(piaybook)
  • 活动星投票午间修身自习室制作在线投票投票制作网页
  • C#泛型:高级静态语言的效率利器
  • 澳大利亚访问学者申请流程总结