map std 浮点数索引_std::map 简介及其使用

注:std::map C++11标准

map概述

template < class Key, //map::key_type

class T, //map::mapped_type

class Compare = less, //map::key_compare

class Alloc = allocator > //map::allocator_type

> class map;

Map是一种关联容器,它按照特定顺序存储由键值Key和映射值Value组合而成的元素。

在map中,键值Key通常用于排序和唯一标识元素,而映射值Value存储与此键值Key相关联的内容。键Key和映射值Value的类型可能不同,并在成员类型value_type中组合在一起,value_type是一个组合了这两种类型的pair类型:typedef pair value_type。

在map内部,元素总是按照其键Key进行排序的,排序时使用其内部比较对象(Compare)并遵循指定的严格弱序准则。

Map容器通常比unordered_map容器在按键Key访问元素的速度上要慢,但它们允许根据子集的顺序直接迭代子集。

Map中的映射值可以通过对应的键Key使用括号操作符(operator[])直接访问。

Map通常是基于二叉搜索树实现的。

容器属性

关联

关联容器中的元素由其键引用,而不是由它们在容器中的绝对位置引用。

有序

容器中的元素始终遵循严格的顺序。所有插入的元素都按这个顺序给定一个位置。

映射

每个元素都将一个键关联到一个映射值:键用于标识其内容为映射值的元素。

键值唯一

容器中没有两个元素可以具有等效键。

分配器

容器使用allocator对象来动态处理其存储需求。

模板参数

Key

键的类型。Map中的每个元素都由其键值唯一标识。别名为成员类型map::key_type。

T

映射值的类型。Map中的每个元素都将一些数据存储为其映射值。别名为成员类型map::mapped_type

Compare

一个二进制谓词,它接受两个键值作为参数并返回一个bool值。表达式 comp(a, b) 中,comp是Compare类型的对象,a和b是键值,如果在函数定义的严格弱序中,a被认为在b之前,则表达式comp(a, b)应该返回true。Map对象使用这个表达式来确定容器中元素的顺序以及两个元素的键是否相等。Map容器中的任何两个元素都不能具有相同的键。Compare可以是函数指针或函数对象,默认为 less,其返回与使用小于操作符(a

Alloc

分配器对象的类型,用于定义存储分配模型。默认情况下,使用allocator类模板,该模板定义最简单的内存分配模型,并且与值无关。别名为成员类型map::allocator_type。

成员类型

member typedefinitionnotes

key_type

The first template parameter (Key)

mapped_type

The second template parameter (T)

value_type

pair

key_compare

The third template parameter (Compare)

defaults to: less

value_compare

Nested function class to compare elements

see value_comp

allocator_type

The fourth template parameter (Alloc)

defaults to: allocator

reference

value_type&

const_reference

const value_type&

pointer

allocator_traits::pointer

for the default allocator: value_type*

const_pointer

allocator_traits::const_pointer

for the default allocator: const value_type*

iterator

a bidirectional iterator to value_type

convertible to const_iterator

const_iterator

a bidirectional iterator to const value_type

reverse_iterator

reverse_iterator

const_reverse_iterator

reverse_iterator

difference_type

a signed integral type, identical to:

iterator_traits::difference_type

usually the same as ptrdiff_t

size_type

an unsigned integral type that can represent any non-negative value of difference_type

usually the same as size_t

成员函数

构造函数(constructor)

empty (1)

explicit map (const key_compare& comp = key_compare(),

const allocator_type& alloc = allocator_type());

explicit map (const allocator_type& alloc);

range (2)

template

map (InputIterator first, InputIterator last,

const key_compare& comp = key_compare(),

const allocator_type& = allocator_type());

copy (3)

map (const map& x);

map (const map& x, const allocator_type& alloc);

move (4)

map (map&& x);

map (map&& x, const allocator_type& alloc);

initializer list (5)

map (initializer_list il,

const key_compare& comp = key_compare(),

const allocator_type& alloc = allocator_type());

构造函数示例:

//constructing maps

#include #include

bool fncomp (char lhs, char rhs) {return lhs

};intmain ()

{

std::mapfirst;

first['a']=10;

first['b']=30;

first['c']=50;

first['d']=70;

std::mapsecond (first.begin(),first.end());

std::mapthird (second);

std::map fourth; //class as Compare

bool(*fn_pt)(char,char) =fncomp;

std::map fifth (fn_pt); //function pointer as Compare

return 0;

}

构造函数示例

析构函数(destructor)

~map();

赋值(operator=)

copy (1)

map& operator= (const map& x);

move (2)

map& operator

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值