C++中map的概念,与简单操作
map<Key, Data, Compare, Alloc>
map是一种关联容器,存储相结合形成的一个关键值和映射值的元素。Map 是一种Pair Associative Container,意味着它的值类型为 pair<const Key, Data>. 而且也是 Unique Associative Container, 也就是任何两个元素没有相同的key值。
map具有重要的属性,就是在map对象中插入一个新元素不指向现有元素的迭代器失效。从map上删除一个元素,也没有任何迭代器失效,除非,当然,实际上指向正在被删除的元素的迭代器。
1、例子
- struct ltstr
- {
- bool operator()(const char* s1, const char* s2) const
- {
- return strcmp(s1, s2) < 0;
- }
- };
- int main()
- {
- map<const char*, int, ltstr> months;
- months["january"] = 31;
- months["february"] = 28;
- months["march"] = 31;
- months["april"] = 30;
- months["may"] = 31;
- months["june"] = 30;
- months["july"] = 31;
- months["august"] = 31;
- months["september"] = 30;
- months["october"] = 31;
- months["november"] = 30;
- months["december"] = 31;
- cout << "june -> " << months["june"] << endl;
- map<const char*, int, ltstr>::iterator cur = months.find("june");
- map<const char*, int, ltstr>::iterator prev = cur;
- map<const char*, int, ltstr>::iterator next = cur;
- ++next;
- --prev;
- cout << "Previous (in alphabetical order) is " << (*prev).first << endl;
- cout << "Next (in alphabetical order) is " << (*next).first << endl;
- }
2、定义形式
- template < class Key, class T, class Compare = less<Key>,
- class Allocator = allocator<pair<const Key,T> > > class map;
3、模板参数具有以下涵义:
key:关键值的类型。在map对象中的每个元素是通过该关键值唯一确定元素的。
T:映射值的类型。在map中的每个元素是用来储存一些数据作为其映射值。
compare:Comparison类:A类键的类型,它有两个参数,并返回一个bool。表达comp(A,B),comp是这比较类A和B是关键值的对象,应返回true,如果是在早先的立场比B放置在一个严格弱排序操作。这可以是一个类实现一个函数调用运算符或一个函数的指针(见一个例子构造)。默认的对于<KEY>,返回申请小于操作符相同的默认值(A <B)。
Map对象使用这个表达式来确定在容器中元素的位置。以下这个规则在任何时候都排列在map容器中的所有元素。
Allocator:用于定义存储分配模型分配器对象的类型。默认情况下,分配器类模板,它定义了最简单的内存分配模式,是值独立的
- map<Key,T>::iterator it;
- (*it).first; // 指向key值(of type Key)
- (*it).second; // 映射的值(of type T)
- (*it); // the "element value" (of type pair<const Key,T>) //看到此处懵逼啊有木有!!(*it)到底该如何表述,查CPLUSPLUS pair 如下:
- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- class template
- <utility>
- std::pair
- template <class T1, class T2> struct pair;Pair of values
This class couples together a pair of values, which may be of different types (T1 and T2). The individual values can be accessed through its public members first and second. 是一对值
Pairs are a particular case of tuple. pairs是一特定的元祖,然后我搜了tuple,
- class template<tuple>
std::tuple
template <class... Types> class tuple;
TupleA tuple is an object capable to hold a collection of elements. Each element can be of a different type. tuple是一个能够容纳元素集合的obiect。每个元素可以是不同的类型。
- it本身是迭代器, (*it)默认指迭代器内部第一个键值对(tuple元组) //尼玛,看半天才知道此处专业叫法是迭代器里边的‘解引用操作符’,指向其内部元素!!
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
也可以如下表达:
- it->first; // same as (*it).first (the key value)
- it->second; // same as (*it).second (the mapped value)
4、成员变量和成员函数
| Member | Where defined | Description |
|---|---|---|
| key_type | Associative Container | map中的key类型 |
| data_type | Pair Associative Container | key关联的值类型 |
| value_type | Pair Associative Container | 对象类型, pair<const key_type, data_type>,存储在map中 |
| key_compare | Sorted Associative Container | Function object 通过顺序比较 |
| value_compare | Sorted Associative Container | Function object that compares two values for ordering. |
| pointer | Container | Pointer to T. |
| reference | Container | Reference to T |
| const_reference | Container | Const reference to T |
| size_type | Container | An unsigned integral type. |
| difference_type | Container | A signed integral type. |
| iterator | Container | Iterator used to iterate through a map. [1] |
| const_iterator | Container | Const iterator used to iterate through a map. |
| reverse_iterator | Reversible Container | Iterator used to iterate backwards through a map.[1] |
| const_reverse_iterator | Reversible Container | Const iterator used to iterate backwards through amap. |
| iterator begin() | Container | Returns an iterator pointing to the beginning of the map. |
| iterator end() | Container | Returns an iterator pointing to the end of the map. |
| const_iterator begin() const | Container | Returns a const_iterator pointing to the beginning of themap. |
| const_iterator end() const | Container | Returns a const_iterator pointing to the end of the map. |
| reverse_iterator rbegin() | Reversible Container | Returns a reverse_iterator pointing to the beginning of the reversed map. |
| reverse_iterator rend() | Reversible Container | Returns a reverse_iterator pointing to the end of the reversed map. |
| const_reverse_iterator rbegin() const | Reversible Container | Returns a const_reverse_iterator pointing to the beginning of the reversed map. |
| const_reverse_iterator rend() const | Reversible Container | Returns a const_reverse_iterator pointing to the end of the reversed map. |
| size_type size() const | Container | Returns the size of the map. |
| size_type max_size() const | Container | Returns the largest possible size of the map. |
| bool empty() const | Container | true if the map's size is 0. |
| key_compare key_comp() const | Sorted Associative Container | Returns the key_compare object used by the map. |
| value_compare value_comp() const | Sorted Associative Container | Returns the value_compare object used by the map. |
| map() | Container | Creates an empty map. |
| map(const key_compare& comp) | Sorted Associative Container | Creates an empty map, using comp as thekey_compare object. |
template <class InputIterator> |
Unique Sorted Associative Container | Creates a map with a copy of a range. |
template <class InputIterator> |
Unique Sorted Associative Container | Creates a map with a copy of a range, using compas thekey_compare object. |
| map(const map&) | Container | The copy constructor. |
| map& operator=(const map&) | Container | The assignment operator |
| void swap(map&) | Container | Swaps the contents of two maps. |
pair<iterator, bool> |
Unique Associative Container | Inserts x into the map. |
iterator insert(iterator pos, |
Unique Sorted Associative Container | Inserts x into the map, using pos as a hint to where it will be inserted. |
template <class InputIterator> |
Unique Sorted Associative Container | Inserts a range into the map. |
| void erase(iterator pos) | Associative Container | Erases the element pointed to by pos. |
| size_type erase(const key_type& k) | Associative Container | Erases the element whose key is k. |
| void erase(iterator first, iterator last) | Associative Container | Erases all elements in a range. |
| void clear() | Associative Container | Erases all of the elements. |
| iterator find(const key_type& k) | Associative Container | Finds an element whose key is k. |
| const_iterator find(const key_type& k) const | Associative Container | Finds an element whose key is k. |
| size_type count(const key_type& k) | Unique Associative Container | Counts the number of elements whose key is k. |
| iterator lower_bound(const key_type& k) | Sorted Associative Container | Finds the first element whose key is not less thank. |
| const_iterator lower_bound(const key_type& k) const | Sorted Associative Container | Finds the first element whose key is not less thank. |
| iterator upper_bound(const key_type& k) | Sorted Associative Container | Finds the first element whose key greater than k. |
| const_iterator upper_bound(const key_type& k) const | Sorted Associative Container | Finds the first element whose key greater than k. |
pair<iterator, iterator> |
Sorted Associative Container | Finds a range containing all elements whose key isk. |
pair<const_iterator, const_iterator> |
Sorted Associative Container | Finds a range containing all elements whose key isk. |
data_type& |
map | See below. |
bool operator==(const map&, |
Forward Container | Tests two maps for equality. This is a global function, not a member function. |
bool operator<(const map&, |
Forward Container | Lexicographical comparison. This is a global function, not a member function. |
- 下面展示了常用的一些方法。<p>// stu_map.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <iostream>
- #include <map>
- using namespace std;
- bool fncomp(char lhs,char rhs)
- {
- return lhs<rhs;
- }
- struct classcomp
- {
- bool operator()(const char& lhs,const char& rhs)
- {
- return lhs<rhs;
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- map<char,int> mymap;
- mymap['a']=10;
- mymap['b']=60;
- mymap['c']=30;
- mymap['d']=90;
- mymap['e']=50;
- map<char,int> second(mymap);
- map<char,int> third(mymap.begin(),mymap.end());
- map<char,int,classcomp> fourth;
- bool(*fn_pt)(char,char)=fncomp;
- map<char,int,bool(*)(char,char)> fifth(fn_pt);
- map<char,int>::key_compare key_comp;
- map<char,int>::iterator it;
- it=mymap.begin();
- for (it;it!=mymap.end();it++)
- {
- cout<<it->first<<":"<<it->second<<endl;
- }
- cout<<"================================="<<endl;
- second.clear();
- second['a']=1002;
- second['b']=10023;
- while (!second.empty())
- {
- cout << second.begin()->first << " => ";
- cout << second.begin()->second << endl;
- second.erase(second.begin());
- }
- cout<<"================================="<<endl;
- mymap.insert(pair<char,int>('f',100) );
- mymap.insert(pair<char,int>('g',200) );
- cout<<"f => " <<mymap.find('f')->second<<endl;
- cout<<"g => " <<mymap.find('g')->second<<endl;
- cout<<"================================="<<endl;
- key_comp=mymap.key_comp();
- cout << "mymap contains:\n";
- char highest=mymap.rbegin()->first; // key value of last element
- it=mymap.begin();
- do {
- cout << (*it).first << " => " << (*it).second << endl;
- } while ( key_comp((*it++).first, highest) );
- cout << endl;
- return 0;
- }
- </p>
运行结果:
C++中map的概念,与简单操作的更多相关文章
- MongoDB快速入门学习笔记2 MongoDB的概念及简单操作
1.以下列举普通的关系型数据库和MongoDB数据库简单概念上的区别: 关系型数据库 MongoDB数据库 说明 database database 数据库 table collection 数据库表 ...
- MySQL基本概念以及简单操作
一.MySQL MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MyS ...
- matlab中矩阵的表示与简单操作
原文地址为:matlab矩阵的表示和简单操作 一.矩阵的表示在MATLAB中创建矩阵有以下规则: a.矩阵元素必须在”[ ]”内: b.矩阵的同行元素之间用空格(或”,”)隔开: c.矩阵的行与行之间 ...
- epoll、mysql概念及简单操作
epoll 程序阻塞的过程 假设我们目前运行了三个进程A B C ,如果他们都在处于运行态,那就会被加到一个运行队列中 进程A正在运行socket程序 在linux中有句话,万物皆文件,socket对 ...
- Java中Arrays 与 Collections 的简单操作
import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.C ...
- C++中Map的使用 (个人简单的对于String的使用)
#include<map> #include<iostream> #include<string> using namespace std; int main() ...
- 关于CSS格式与布局中的基础知识的简单操作
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JS Map 和 List 的简单实现代码
javascript中是没有map和list 结构的. 本篇文章是对在JS中Map和List的简单实现代码进行了详细的分析介绍,需要的朋友参考下 代码如下: /* * MAP对象,实现MAP功能 * ...
- C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET
C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET C++ STL中Map的相关排序操作:按Key排序和按Value排序 分类: C ...
随机推荐
- github上一款特别的侧滑
知识分享: 首先看图,我只是大自然的搬运工,想实现这种特效的请点击连接下载github地址忘掉了,....http://download.csdn.net/detail/lj419855402/860 ...
- MySQL系列——几个常用的mysql命令
1:使用SHOW语句找出在服务器上当前存在什么数据库:mysql> SHOW DATABASES;2:2.创建一个数据库MYSQLDATAmysql> CREATE DATABASE MY ...
- 20135202闫佳歆--week 9 期中总结
期中总结 前半学期的主要学习内容是学习mooc课程<Linux内核分析>以及课本<Linux内核设计与实现>. 所涉及知识点总结如下: 1. Linux内核启动的过程--以Me ...
- Linux基础入门(20135207 王国伊)
实验一 Linux系统简介 一.实验心得 首个实验是简单介绍了Linux系统的简介,了解Linux系统的历史和发展.使我受益匪浅 实验二 基本概念及操作 一.学习目标 1.实验楼环境介绍 2.常用 ...
- IOS开发之——意见反馈UITextView的使用+不能输入字符输入
@interface DMFeedbackViewController ()<UITextViewDelegate,UIAlertViewDelegate>@property (nonat ...
- nodeJs--模块module.exports与实例化方法;
在nodejs中,提供了exports 和 require 两个对象,其中 exports 是模块公开的接口,require 用于从外部获取一个模块的接口,即所获取模块的 exports 对象.而在e ...
- Opencv step by step - 自适应阈值
上个博客提到的阈值化只是针对图像全局进行阈值化,opencv提供了一个更好的函数cvAdaptiveThreshold,可以做到局部特征的阈值化,这样一来, 整个图像的信息可以被更好的提取. #inc ...
- 实验箱FPGA部分测试报告及A8与FPGA链接测试报告
其实,我一开始还以为实验箱不会有什么问题只是让我们多学习东西才做这个测试的,结果发现还真的有不少问题. 1.实验准备部分 安装驱动时,win10系统无法正确安装usb-blaster Windows ...
- 开发一个简单实用的android紧急求助软件
之前女朋友一个人住,不怎么放心,想找一个紧急求助的软件,万一有什么突发情况,可以立即知道.用金山手机卫士的手机定位功能可以知道对方的位置状态,但不能主动发送求助信息,在网上了很多的APK,都是鸡肋功能 ...
- [整理] ES5 词法约定文档树状图
将ES5 词法说明整理为了树状图,方便查阅,请自行点开小图看大图: