定义于头文件
template< class Key, | (1) | |
namespace pmr { template | (2) | (C++17 起) |
std::map
是有序键值对容器,它的元素的键是唯一的。用比较函数 Compare
排序键。搜索、移除和插入操作拥有对数复杂度。 map 通常实现为红黑树。
在每个标准库使用比较 (Compare) 概念的位置,以等价关系检验唯一性。不精确而言,若二个对象 a
与 b
互相比较不小于对方 : !comp(a, b) && !comp(b, a)
,则认为它们等价(非唯一)。
std::map
满足容器 (Container) 、具分配器容器 (AllocatorAwareContainer) 、关联容器 (AssociativeContainer) 和可逆容器 (ReversibleContainer) 的要求。
std::map<Key,T,Compare,Allocator>::empty
bool empty() const; | (C++11 前) | |
bool empty() const noexcept; | (C++11 起) (C++20 前) | |
[[nodiscard]] bool empty() const noexcept; | (C++20 起) |
检查容器是否无元素,即是否 begin() == end() 。
(无)
若容器为空则为 true ,否则为 false
常数。
std::map<Key,T,Compare,Allocator>::size
size_type size() const; | (C++11 前) | |
size_type size() const noexcept; | (C++11 起) |
返回容器中的元素数,即 std::distance(begin(), end()) 。
(无)
容器中的元素数量。
常数。
std::map<Key,T,Compare,Allocator>::max_size
size_type max_size() const; | (C++11 前) | |
size_type max_size() const noexcept; | (C++11 起) |
返回根据系统或库实现限制的容器可保有的元素最大数量,即对于最大容器的 std::distance(begin(), end()) 。
(无)
元素数量的最大值。
常数。
此值通常反映容器大小上的理论极限,至多为 std::numeric_limitsmax_size()
的值。
- #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()
- {
- auto genKey = []()
- {
- return std::rand() % 10 + 100;
- };
-
- auto generate = []()
- {
- int n = std::rand() % 10 + 100;
- Cell cell{n, n};
- return cell;
- };
-
- std::map<int, Cell> map1;
- //检查容器是否无元素,即是否 begin() == end() 。
- std::cout << "map1 is empty: " << map1.empty() << std::endl;
-
- for (size_t index = 0; index < 5; index++)
- {
- map1.insert({genKey(), generate()});
- }
- std::cout << "map1: ";
- std::copy(map1.begin(), map1.end(),
- std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
- std::cout << std::endl;
-
- //检查容器是否无元素,即是否 begin() == end() 。
- std::cout << "map1 is empty: " << map1.empty() << std::endl;
- std::cout << std::endl;
- std::cout << std::endl;
-
- std::map<int, Cell> map2;
- for (size_t index = 0; index < 6; index++)
- {
- //返回容器中的元素数,即 std::distance(begin(), end()) 。
- std::cout << "map2 size : " << map2.size() << ": ";
- std::copy(map2.begin(), map2.end(),
- std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
- std::cout << std::endl;
- map2.insert({genKey(), generate()});
- }
- std::cout << std::endl;
-
-
- //返回根据系统或库实现限制的容器可保有的元素最大数量,即对于最大容器的 std::distance(begin(), end()) 。
- std::map<bool, bool> mapbb;
- std::cout << "map
max_size: " << mapbb.max_size() << std::endl; - std::map<char, char> mapcc;
- std::cout << "map
max_size: " << mapcc.max_size() << std::endl; - std::map<short, short> mapss;
- std::cout << "map
max_size: " << mapss.max_size() << std::endl; - std::map<unsigned short, unsigned short> mapusus;
- std::cout << "map
max_size: " << mapusus.max_size() << std::endl; - std::map<int, int> mapii;
- std::cout << "map
max_size: " << mapii.max_size() << std::endl; - std::map<uint8_t, uint8_t> mapui8ui8;
- std::cout << "map
max_size: " << mapui8ui8.max_size() << std::endl; - std::map<uint16_t, uint16_t> mapui16ui16;
- std::cout << "map
max_size: " << mapui16ui16.max_size() << std::endl; - std::map<uint32_t, uint32_t> mapui32ui32;
- std::cout << "map
max_size: " << mapui32ui32.max_size() << std::endl; - std::map<uint64_t, uint64_t> mapui64ui64;
- std::cout << "map
max_size: " << mapui64ui64.max_size() << std::endl; - std::map<double, double> mapdd;
- std::cout << "map
max_size: " << mapdd.max_size() << std::endl; - std::map<float, float> mapff;
- std::cout << "map
max_size: " << mapff.max_size() << std::endl; - std::map<long, long> mapll;
- std::cout << "map
max_size: " << mapll.max_size() << std::endl; - std::map<long long, long long> mapllll;
- std::cout << "map
max_size: " << mapllll.max_size() << std::endl; - std::map<string, string> mapstrstr;
- std::cout << "map
max_size: " << mapstrstr.max_size() << std::endl; - std::map<Cell, Cell> mapcell;
- std::cout << "map
max_size: " | << mapcell.max_size() << std::endl; -
- return 0;
- }