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

定义于头文件

template<

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

> class map;
(1)
namespace pmr {

    template >
    using map = std::map                          std::pmr::polymorphic_allocator>>

}
(2) (C++17 起)

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

在每个标准库使用比较 (Compare) 概念的位置,以等价关系检验唯一性。不精确而言,若二个对象 ab 互相比较不小于对方 : !comp(a, b) && !comp(b, a) ,则认为它们等价(非唯一)。

std::map 满足容器 (Container) 、具分配器容器 (AllocatorAwareContainer) 、关联容器 (AssociativeContainer) 和可逆容器 (ReversibleContainer) 的要求。

容量

检查容器是否为空

std::map::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::size

size_type size() const;

(C++11 前)

size_type size() const noexcept;

(C++11 起)

 返回容器中的元素数,即 std::distance(begin(), end()) 。

参数

(无)

返回值

容器中的元素数量。

复杂度

常数。

返回可容纳的最大元素数

std::map::max_size

size_type max_size() const;

(C++11 前)

size_type max_size() const noexcept;

(C++11 起)

返回根据系统或库实现限制的容器可保有的元素最大数量,即对于最大容器的 std::distance(begin(), end()) 。

参数

(无)

返回值

元素数量的最大值。

复杂度

常数。

注意

此值通常反映容器大小上的理论极限,至多为 std::numeric_limits::max() 。运行时,可用 RAM 总量可能会限制容器大小到小于 max_size() 的值。

 

调用示例

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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 &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 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::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 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::cout, " "));
        std::cout << std::endl;
        map2.insert({genKey(), generate()});
    }
    std::cout << std::endl;


    //返回根据系统或库实现限制的容器可保有的元素最大数量,即对于最大容器的 std::distance(begin(), end()) 。
    std::map mapbb;
    std::cout << "map max_size:                        " << mapbb.max_size() << std::endl;
    std::map mapcc;
    std::cout << "map max_size:                        " << mapcc.max_size() << std::endl;
    std::map mapss;
    std::cout << "map max_size:                      " << mapss.max_size() << std::endl;
    std::map mapusus;
    std::cout << "map max_size:    " << mapusus.max_size() << std::endl;
    std::map mapii;
    std::cout << "map max_size:                          " << mapii.max_size() << std::endl;
    std::map mapui8ui8;
    std::cout << "map max_size:                  " << mapui8ui8.max_size() << std::endl;
    std::map mapui16ui16;
    std::cout << "map max_size:                " << mapui16ui16.max_size() << std::endl;
    std::map mapui32ui32;
    std::cout << "map max_size:                " << mapui32ui32.max_size() << std::endl;
    std::map mapui64ui64;
    std::cout << "map max_size:                " << mapui64ui64.max_size() << std::endl;
    std::map mapdd;
    std::cout << "map max_size:                    " << mapdd.max_size() << std::endl;
    std::map mapff;
    std::cout << "map max_size:                      " << mapff.max_size() << std::endl;
    std::map mapll;
    std::cout << "map max_size:                        " << mapll.max_size() << std::endl;
    std::map mapllll;
    std::cout << "map max_size:              " << mapllll.max_size() << std::endl;
    std::map mapstrstr;
    std::cout << "map max_size:                    " << mapstrstr.max_size() << std::endl;
    std::map mapcell;
    std::cout << "map max_size:                        " << mapcell.max_size() << std::endl;

    return 0;
}

输出

c++ 11标准模板(STL) std::map(六)_第1张图片

 

你可能感兴趣的:(#,std::map,c++,标准库模板,map,有序键值对容器,容量)