c++11 标准模板(STL)(std::multimap)(四)
定义于头文件 <map>
template< class Key, | (1) | |
namespace pmr { template <class Key, class T, class Compare = std::less<Key>> | (2) | (C++17 起) |
multimap 是关联容器,含有关键-值 pair 的已排序列表,同时容许多个入口拥有同一关键。按照应用到关键的比较函数 Compare
排序。搜索、插入和移除操作拥有对数复杂度。
拥有等价关键的关键-值 pair 的顺序就是插入顺序,且不会更改。(C++11 起)
凡在标准库使用比较 (Compare) 概念出,都用描述于比较 (Compare) 上的等价关系确定等价性。不精确地说,若二个对象 a
和 b
互不小于对方: !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;
}
输出