c++ 11标准模板(STL) std::map(六)

定义于头文件<map>

template<

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

> class map;
(1)
namespace pmr {

    template <class Key, class T, class Compare = std::less<Key>>
    using map = std::map<Key, T, Compare,
                         std::pmr::polymorphic_allocator<std::pair<const Key,T>>>

}
(2) (C++17 起)

 std::map 是有序键值对容器,它的元素的键是唯一的。用比较函数 Compare 排序键。搜索、移除和插入操作拥有对数复杂度。 map 通常实现为红黑树

在每个标准库使用比较 (Compare) 概念的位置,以等价关系检验唯一性。不精确而言,若二个对象 ab 互相比较不小于对方 : !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_limits<difference_type>::max() 。运行时,可用 RAM 总量可能会限制容器大小到小于 max_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<bool, bool> max_size:                        " << mapbb.max_size() << std::endl;
    std::map<char, char> mapcc;
    std::cout << "map<char, char> max_size:                        " << mapcc.max_size() << std::endl;
    std::map<short, short> mapss;
    std::cout << "map<short, short> max_size:                      " << mapss.max_size() << std::endl;
    std::map<unsigned short, unsigned short> mapusus;
    std::cout << "map<unsigned short, unsigned short> max_size:    " << mapusus.max_size() << std::endl;
    std::map<int, int> mapii;
    std::cout << "map<int, int> max_size:                          " << mapii.max_size() << std::endl;
    std::map<uint8_t, uint8_t> mapui8ui8;
    std::cout << "map<uint8_t, uint8_t> max_size:                  " << mapui8ui8.max_size() << std::endl;
    std::map<uint16_t, uint16_t> mapui16ui16;
    std::cout << "map<uint16_t, uint16_t> max_size:                " << mapui16ui16.max_size() << std::endl;
    std::map<uint32_t, uint32_t> mapui32ui32;
    std::cout << "map<uint32_t, uint32_t> max_size:                " << mapui32ui32.max_size() << std::endl;
    std::map<uint64_t, uint64_t> mapui64ui64;
    std::cout << "map<uint64_t, uint64_t> max_size:                " << mapui64ui64.max_size() << std::endl;
    std::map<double, double> mapdd;
    std::cout << "map<double, double> max_size:                    " << mapdd.max_size() << std::endl;
    std::map<float, float> mapff;
    std::cout << "map<float, float> max_size:                      " << mapff.max_size() << std::endl;
    std::map<long, long> mapll;
    std::cout << "map<long, long> max_size:                        " << mapll.max_size() << std::endl;
    std::map<long long, long long> mapllll;
    std::cout << "map<long long, long long> max_size:              " << mapllll.max_size() << std::endl;
    std::map<string, string> mapstrstr;
    std::cout << "map<string, string> max_size:                    " << mapstrstr.max_size() << std::endl;
    std::map<Cell, Cell> mapcell;
    std::cout << "map<Cell, Cell> max_size:                        " << mapcell.max_size() << std::endl;

    return 0;
}

输出

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/130906982