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>::begin, 
std::map<Key,T,Compare,Allocator>::cbegin

iterator begin();

(C++11 前)

iterator begin() noexcept;

(C++11 起)

const_iterator begin() const;

(C++11 前)

const_iterator begin() const noexcept;

(C++11 起)

const_iterator cbegin() const noexcept;

(C++11 起)

 返回指向容器首元素的迭代器。

若容器为空,则返回的迭代器将等于 end() 。

参数

(无)

返回值

指向首元素的迭代器。

复杂度

常数。

返回指向容器尾端的迭代器

std::map<Key,T,Compare,Allocator>::end, 
std::map<Key,T,Compare,Allocator>::cend

iterator end();

(C++11 前)

iterator end() noexcept;

(C++11 起)

const_iterator end() const;

(C++11 前)

const_iterator end() const noexcept;

(C++11 起)

const_iterator cend() const noexcept;

(C++11 起)

返回指向容器末元素后一元素的迭代器。

此元素表现为占位符;试图访问它导致未定义行为。

参数

(无)

返回值

指向后随最后元素的迭代器。

复杂度

常数。

返回指向容器最后元素的逆向迭代器

std::map<Key,T,Compare,Allocator>::rbegin, 
std::map<Key,T,Compare,Allocator>::crbegin

reverse_iterator rbegin();

(C++11 前)

reverse_iterator rbegin() noexcept;

(C++11 起)

const_reverse_iterator rbegin() const;

(C++11 前)

const_reverse_iterator rbegin() const noexcept;

(C++11 起)

const_reverse_iterator crbegin() const noexcept;

(C++11 起)

返回指向逆向容器首元素的逆向迭代器。它对应非逆向容器的末元素。

参数

(无)

返回值

指向首元素的逆向迭代器。

复杂度

常数。

返回指向前端的逆向迭代器

std::map<Key,T,Compare,Allocator>::rend, 
std::map<Key,T,Compare,Allocator>::crend

reverse_iterator rend();

(C++11 前)

reverse_iterator rend() noexcept;

(C++11 起)

const_reverse_iterator rend() const;

(C++11 前)

const_reverse_iterator rend() const noexcept;

(C++11 起)

const_reverse_iterator crend() const noexcept;

(C++11 起)

 返回指向逆向容器末元素后一元素的逆向迭代器。它对应非逆向容器首元素的前一元素。此元素表现为占位符,试图访问它导致未定义行为。

参数

(无)

返回值

指向末元素后一元素的逆向迭代器。

复杂度

常数。

调用示例

#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::multimap<int, Cell> multimap1;
    for (size_t index = 0; index < 5; index++)
    {
        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;

    //返回指向容器首元素的迭代器。若容器为空,则返回的迭代器将等于 end() 。
    //返回指向容器末元素后一元素的迭代器。此元素表现为占位符;试图访问它导致未定义行为。
    //返回指向逆向容器首元素的逆向迭代器。它对应非逆向容器的末元素。
    //返回指向逆向容器末元素后一元素的逆向迭代器。它对应非逆向容器首元素的前一元素。
    //此元素表现为占位符,试图访问它导致未定义行为。

    std::cout << "const_iterator:           ";
    for (std::multimap<int, Cell>::const_iterator
            cit = multimap1.cbegin(); cit != multimap1.cend(); cit++)
    {
        std::cout << cit->first << "-" << cit->second << " ";
    }
    std::cout << std::endl;

    std::cout << "const_reverse_iterator:   ";
    for (std::multimap<int, Cell>::const_reverse_iterator
            crit = multimap1.crbegin(); crit != multimap1.crend(); crit++)
    {
        std::cout << crit->first << "-" << crit->second << " ";
    }
    std::cout << std::endl;

    std::cout << "iterator:                 ";
    for (std::multimap<int, Cell>::iterator it = multimap1.begin(); it != multimap1.end(); it++)
    {
        it->second = generate();
        std::cout << it->first << "-" << it->second << " ";
    }
    std::cout << std::endl;

    std::cout << "reverse_iterator:         ";
    for (std::multimap<int, Cell>::reverse_iterator
            rit = multimap1.rbegin(); rit != multimap1.rend(); rit++)
    {
        rit->second = generate();
        std::cout << rit->first << "-" << rit->second << " ";
    }
    std::cout << std::endl;

    return 0;
}

输出

猜你喜欢

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