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>::insert

std::pair<iterator,bool> insert( const value_type& value );

(1)

template< class P >
std::pair<iterator,bool> insert( P&& value );

(2) (C++11 起)

std::pair<iterator,bool> insert( value_type&& value );

(3) (C++17 起)

iterator insert( iterator hint, const value_type& value );

(4) (C++11 前)

iterator insert( const_iterator hint, const value_type& value );

(C++11 起)

template< class P >
iterator insert( const_iterator hint, P&& value );

(5) (C++11 起)

iterator insert( const_iterator hint, value_type&& value );

(6) (C++17 起)

template< class InputIt >
void insert( InputIt first, InputIt last );

(7)

void insert( std::initializer_list<value_type> ilist );

(8) (C++11 起)

insert_return_type insert(node_type&& nh);

(9) (C++17 起)

iterator insert(const_iterator hint, node_type&& nh);

(10) (C++17 起)

若容器尚未含有带等价关键的元素,则插入元素到容器中。

1-3) 插入 value 。重载 (2) 等价于 emplace(std::forward<P>(value)) ,且仅若 std::is_constructible<value_type, P&&>::value == true 才参与重载决议。

4-6) 插入 value 到尽可能接近,恰好前于(C++11 起) hint 的位置。重载 (4) 等价于 emplace_hint(hint, std::forward<P>(value)) ,且仅若 std::is_constructible<value_type, P&&>::value == true 才参与重载决议。

7) 插入来自范围 [first, last) 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的(待决的 LWG2844 )。

8) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的(待决的 LWG2844 )。

9) 若 nh 是空的结点把柄,则不做任何事。否则插入 nh 所占有的元素到容器,若容器尚未含有拥有等价于 nh.key() 的关键的元素。若 nh 非空且 get_allocator() != nh.get_allocator() 则行为未定义。

10) 若 nh 是空的结点把柄,则不做任何事并返回尾迭代器。否则,插入 nh 所占有的元素到容器,若容器尚未含有拥有等价于 nh.key() 的关键的元素,并返回指向拥有等于 nh.key() 的关键的元素的迭代器(无关乎插入成功还是失败)。若插入成功,则从 nh 移动,否则它保持该元素的所有权。元素被插入到尽可能接近正好先于 hint 的位置。若 nh 非空且 get_allocator() != nh.get_allocator() 则行为未定义。

没有迭代器或引用被非法化。若插入成功,则在结点把柄保有元素时获得的指向该元素的指针和引用被非法化,而在提取前获得的指向元素的指针和引用变得合法。 (C++17 起)

参数

hint -

用作搜索开始位置的建议的迭代器

(C++11 前)

指向将插入新元素到其前的位置的迭代器

(C++11 起)
value - 要插入的值
first, last - 要插入的元素范围
ilist - 插入值来源的 initializer_list
nh - 兼容的结点把柄
类型要求
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。

返回值

1-3) 返回由指向被插入元素的迭代器(或阻止插入的元素的迭代器)和指代插入是否发生的 bool 组成的 pair 。

4-6) 返回指向被插入元素的迭代器,或指向阻止插入的元素的迭代器。

7-8) (无)

9) 返回 insert_return_type ,其成员初始化如下:若 nh 为空,则 insertedfalseposition 为 end() ,而 node 为空。否则发生插入, insertedtrueposition 指向被插入元素,而 node 为空。若插入失败,则 insertedfalsenode 拥有 nh 的先前值,而 position 指向拥有等价于 nh.key() 的关键的元素。

10) 若 nh 为空则为尾迭代器,若插入发生则为指向被插入元素的迭代器,而若插入失败则为指向拥有等价于 nh.key() 的关键的元素的迭代器。

异常

1-6) 若任何操作抛出异常,则插入无效果(强异常保证)。

7-8) 若任何操作抛出异常,则程序在合法状态(基础异常保证)。

9-10) 若任何操作抛出异常,则插入无效果, nh 保持不变(强异常保证)。

复杂度

1-3) 与容器大小成对数, O(log(size()))

4-6) 若插入恰好发生在 hint 的位置则为均摊常数,否则与容器大小成对数。

(C++11 前)

4-6) 若插入恰好发生在 hint 的位置则为均摊常数,否则与容器大小成对数。

(C++11 起)

7-8) O(N*log(size() + N)) ,其中 N 是要插入的元素数。

9) 与容器大小成对数, O(log(size()))

10) 若插入恰好发生在 hint 的位置则为均摊常数,否则与容器大小成对数。

注解

有提示插入 (4-6) 不返回 bool ,这是为了与顺序容器上的定位插入,如 std::vector::insert 签名兼容。这使得可以创建泛型插入器,例如 std::inserter 。检查有提示插入是否成功的一种方式是比较插入前后的 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;
    for (size_t index = 0; index < 5; index++)
    {
        //1-3) 插入 value 。若容器尚未含有带等价关键的元素,则插入元素到容器中。
        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;
    }
    std::cout << std::endl;


    std::map<int, Cell> map2;
    for (size_t index = 0; index < 5; index++)
    {
        //1-3) 插入 value 。若容器尚未含有带等价关键的元素,则插入元素到容器中。移动语义
        std::pair<int, Cell> tpair{genKey(), generate()};
        map2.insert(std::move(tpair));
        std::cout << "map2:    ";
        std::copy(map2.begin(), map2.end(),
                  std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
        std::cout << std::endl;
    }
    std::cout << std::endl;


    std::map<int, Cell> map3;
    for (size_t index = 0; index < 5; index++)
    {
        //4-6) 插入 value 到尽可能接近,恰好前于(C++11 起) hint 的位置。
        map3.insert(map3.begin(), {genKey(), generate()});
        std::cout << "map3:    ";
        std::copy(map3.begin(), map3.end(),
                  std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
        std::cout << std::endl;
    }
    std::cout << std::endl;


    std::map<int, Cell> map4;
    for (size_t index = 0; index < 5; index++)
    {
        //4-6) 插入 value 到尽可能接近,恰好前于(C++11 起) hint 的位置。移动语义
        std::pair<int, Cell> tpair{genKey(), generate()};
        map4.insert(map4.begin(), std::move(tpair));
        std::cout << "map4:    ";
        std::copy(map4.begin(), map4.end(),
                  std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
        std::cout << std::endl;
    }
    std::cout << std::endl;


    std::map<int, Cell> map5;
    for (size_t index = 0; index < 5; index++)
    {
        //7) 插入来自范围 [first, last) 的元素。
        //若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
        map5.insert(map3.begin(), map3.end());
        std::cout << "map5:    ";
        std::copy(map5.begin(), map5.end(),
                  std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
        std::cout << std::endl;
    }
    std::cout << std::endl;


    std::map<int, Cell> map6;
    //6) 插入来自 initializer_list ilist 的元素。
    //若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
    map6.insert({
   
   {genKey(), generate()}, {genKey(), generate()},
        {genKey(), generate()}, {genKey(), generate()}, {genKey(), generate()}});
    std::cout << "map6:    ";
    std::copy(map6.begin(), map6.end(),
              std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
    std::cout << std::endl;
    return 0;
}

输出

猜你喜欢

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