当前位置: 首页 > 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>::begin, 
std::multimap<Key,T,Compare,Allocator>::cbegin

iterator begin();

(C++11 前)

iterator begin() noexcept;

(C++11 起)

const_iterator begin() const;

(C++11 前)

const_iterator begin() const noexcept;

(C++11 起)

const_iterator cbegin() const noexcept;

(C++11 起)

 返回指向容器首元素的迭代器。

若容器为空,则返回的迭代器将等于 end() 。

参数

(无)

返回值

指向首元素的迭代器。

复杂度

常数。

返回指向容器尾端的迭代器

std::multimap<Key,T,Compare,Allocator>::end, 
std::multimap<Key,T,Compare,Allocator>::cend

iterator end();

(C++11 前)

iterator end() noexcept;

(C++11 起)

const_iterator end() const;

(C++11 前)

const_iterator end() const noexcept;

(C++11 起)

const_iterator cend() const noexcept;

(C++11 起)

返回指向容器末元素后一元素的迭代器。

此元素表现为占位符;试图访问它导致未定义行为。 

参数

(无)

返回值

指向后随最后元素的迭代器。

复杂度

常数。

 

返回指向容器最后元素的逆向迭代器

std::multimap<Key,T,Compare,Allocator>::rbegin, 
std::multimap<Key,T,Compare,Allocator>::crbegin

reverse_iterator rbegin();

(C++11 前)

reverse_iterator rbegin() noexcept;

(C++11 起)

const_reverse_iterator rbegin() const;

(C++11 前)

const_reverse_iterator rbegin() const noexcept;

(C++11 起)

const_reverse_iterator crbegin() const noexcept;

(C++11 起)

 返回指向逆向容器首元素的逆向迭代器。它对应非逆向容器的末元素。 

参数

(无)

返回值

指向首元素的逆向迭代器。

复杂度

常数。

返回指向前端的逆向迭代器

std::multimap<Key,T,Compare,Allocator>::rend, 
std::multimap<Key,T,Compare,Allocator>::crend

reverse_iterator rend();

(C++11 前)

reverse_iterator rend() noexcept;

(C++11 起)

const_reverse_iterator rend() const;

(C++11 前)

const_reverse_iterator rend() const noexcept;

(C++11 起)

const_reverse_iterator crend() const noexcept;

(C++11 起)

返回指向逆向容器末元素后一元素的逆向迭代器。它对应非逆向容器首元素的前一元素。此元素表现为占位符,试图访问它导致未定义行为。

参数

(无)

返回值

指向末元素后一元素的逆向迭代器。

复杂度

常数。

调用示例

#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;std::cout << "const_iterator:           ";for (std::multimap<int, Cell>::const_iterator cit = multimap1.cbegin(); cit != multimap1.cend(); cit++){std::cout << cit->first << "-" << cit->second << " ";}std::cout << std::endl;std::cout << "const_reverse_iterator:   ";for (std::multimap<int, Cell>::const_reverse_iterator crit = multimap1.crbegin(); crit != multimap1.crend(); crit++){std::cout << crit->first << "-" << crit->second << " ";}std::cout << std::endl;std::cout << "iterator:                 ";for (std::multimap<int, Cell>::iterator it = multimap1.begin(); it != multimap1.end(); it++){it->second = generate();std::cout << it->first << "-" << it->second << " ";}std::cout << std::endl;std::cout << "reverse_iterator:         ";for (std::multimap<int, Cell>::reverse_iterator it = multimap1.rbegin(); it != multimap1.rend(); it++){it->second = generate();std::cout << it->first << "-" << it->second << " ";}std::cout << std::endl;return 0;
}

 

输出

 

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

相关文章:

  • 乐观锁及悲观锁
  • 常见的锁策略
  • springboot学习(八十) springboot中使用Log4j2记录分布式链路日志
  • 10种ADC软件滤波方法及程序
  • 第五章:Windows server加域
  • Elasticsearch:获取 nested 类型数组中的所有元素
  • English Learning - Day53 作业打卡 2023.2.7 周二
  • SpringMVC--注解配置SpringMVC、SpringMVC执行流程
  • JavaScript中数组常用的方法
  • ModuleNotFoundError: No module named ‘pip‘
  • ROS2 入门应用 发布和订阅(C++)
  • XSS漏洞,通过XSS实现网页挂马
  • 家政服务小程序实战教程09-图文卡片
  • 国内唯一一部在CentOS下正确编译安装和使用RediSearch的教程
  • 前端对于深拷贝和浅拷贝的应用和思考
  • Java基础常见面试题(三)
  • C++设计模式(13)——装饰模式
  • ESP-01S通过AT指令上报数据到阿里云物模型
  • 【强化学习】马尔可夫决策过程MDP
  • 刘润:五维思考,让你站得更高、看得更远
  • 从运维角度看微服务 k8s部署微服务【偏理论】【AL】
  • 专题 | 防抖和节流
  • C++入门:重载运算符和重载函数
  • conda 新建虚拟环境 等等
  • 【C++:STL之栈和队列 | 模拟实现 | 优先级队列 】
  • 基于SpringBoot+Vue的疫苗预约管理系统(Java项目)
  • 华为OD机试 - 计算网络信号(Python),真题含思路
  • 【Spring】注解实现IOC操作,你理解了吗?
  • 微搭低代码从入门到精通01-总体介绍
  • 类的继承