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

定义于头文件 <map>
template<

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

> class multimap;
(1)
namespace pmr {

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

}
(2)(C++17 起)

 multimap 是关联容器,含有关键-值 pair 的已排序列表,同时容许多个入口拥有同一关键。按照应用到关键的比较函数 Compare 排序。搜索、插入和移除操作拥有对数复杂度。

拥有等价关键的关键-值 pair 的顺序就是插入顺序,且不会更改。(C++11 起)

凡在标准库使用比较 (Compare) 概念出,都用描述于比较 (Compare) 上的等价关系确定等价性。不精确地说,若二个对象 ab 互不小于对方: !comp(a, b) && !comp(b, a) ,则认为它们等价。

修改器

插入元素或结点

std::multimap<Key,T,Compare,Allocator>::insert

iterator insert( const value_type& value );

(1)

iterator insert( value_type&& value );

(1)(C++17 起)

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

(2)(C++11 起)

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

(3)(C++11 前)

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

(C++11 起)

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

(3)(C++17 起)

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

(4)(C++11 起)

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

(5)

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

(6)(C++11 起)

iterator insert(node_type&& nh);

(7)(C++17 起)

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

(8)(C++17 起)

 

插入元素到容器中。

1-2) 插入 value 。若容器拥有带等价关键的元素,则插入到范围的上界。(C++11 起)重载 (2) 等价于 emplace(std::forward<P>(value)) ,且仅若 std::is_constructible<value_type, P&&>::value == true 才参与重载决议。

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

5) 插入来自范围 [first, last) 的元素。

6) 插入来自 initializer_list ilist 的元素。

7) 若 nh 是空的结点把柄,则不做任何事。否则插入 nh 所占有的元素到容器并返回指向被插入元素的迭代器。若范围含有关键等价于存在于容器中的 nh.key() 的关键,则在范围结尾插入元素。若 nh 非空且 get_allocator() != nh.get_allocator() 则行为未定义。

8) 若 nh 是空的结点把柄,则不做任何事并返回尾迭代器。否则,插入 nh 所占有的元素到容器,并返回指向拥有等于 nh.key() 的关键的元素的迭代器元素被插入到尽可能接近正好先于 hint 的位置。若 nh 非空且 get_allocator() != nh.get_allocator() 则行为未定义。

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

参数

hint-

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

(C++11 前)

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

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

返回值

1-4) 指向被插入元素的迭代器。

5-6) (无)

7,8) 若 nh 为则为尾迭代器,否则为指向被插入元素的迭代器。

异常

1-4) 若任何操作抛出异常,则插入无效果。

本节未完成
原因:情况 5-6

复杂度

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

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

(C++11 前)

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

(C++11 起)

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

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

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

调用示例

#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++)
    {
        //1-2) 插入 value 。若容器拥有带等价关键的元素。
        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 << std::endl;


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


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


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


    std::multimap<int, Cell> multimap5;
    for (size_t index = 0; index < 5; index++)
    {
        //5) 插入来自范围 [first, last) 的元素。
        multimap5.insert(multimap3.begin(), multimap3.end());
        std::cout << "multimap5:    ";
        std::copy(multimap5.begin(), multimap5.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
        std::cout << std::endl;
    }
    std::cout << std::endl;


    std::multimap<int, Cell> multimap6;
    //6) 插入来自 initializer_list ilist 的元素。
    multimap6.insert({{genKey(), generate()}, {genKey(), generate()},
        {genKey(), generate()}, {genKey(), generate()}, {genKey(), generate()}});
    std::cout << "multimap6:    ";
    std::copy(multimap6.begin(), multimap6.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
    std::cout << std::endl;

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值