Maps是一种关联式容器,包含“关键字/值”对。 Multimaps和maps很相似,但是MultiMaps允许重复的元素。

  简单介绍:

1、声明,首先包含头文件 “map”

    map <int,string> test1,test2;//
map <int,string>::iterator it1,it2;//迭代器
multimap <int,string> test3;
multimap <int,string>::iterator it3;

2、插入数据,可使用三种方法:

  第一种,使用pair函数

   test1.insert(pair<int,string>(,"song"));
test1.insert(pair<int,string>(,"zhang"));
test1.insert(pair<int,string>(,"wang"));

  第二种,使用value_type类型

   test1.insert(map<int,string>::value_type(,"qian"));
test1.insert(map<int,string>::value_type(,"sun"));

  第三种,使用数组方式,,可以覆盖原来数据,前两中不能改变数据,如果存在则插入失败

    test1[] = "mao";
test1[] = "guang";

  前两种情况,插入失败后可以通过以下方法检查

//测试是否插入成功
pair<map<int,string>::iterator,bool> insert_Pair;
insert_Pair = test1.insert(pair<int,string>(,"Replace"));
if (insert_Pair.second == true)
{
cout<<"insert successfully"<<endl;
}
else
{
cout<<"insert failure"<<endl;
}

3、遍历数据,可使用以下几种方法

  第一,正向遍历

    for (it1 = test1.begin();it1 != test1.end();it1++)
{
cout<<it1->first<<"-----" << it1->second<<endl;
}

  第二,逆向遍历,使用反向迭代器

  cout<<"反向迭代器"<<endl;
//rbegin()指向最后一个元素,rend()指向第一个元素前面,这里++是指往前走一个位置
map<int,string>::reverse_iterator reverseIte;
for (reverseIte = test1.rbegin();reverseIte != test1.rend();reverseIte++)
{
cout<<reverseIte->first<<"-----" << reverseIte->second<<endl;
}

  第三,使用数组进行遍历

  //使用数组方式进行遍历
for (int i = ;i <= test1.size();i++)
{
cout<<i<<"-----"<<test1[i]<<endl;
}

4、查找和判断

  第一,使用count进行判断

    //count,判断
int i=;
for (it1 = test1.begin();it1 != test1.end();i++,it1++)
{
cout<<i;
if (test1.count(i)>)//元素存在
{
cout<<"is a element of map"<<endl;
}
else
{
cout<<"is not a element of map"<<endl;
}
}

  第二,使用find判断

  it2 = test1.find();//查找
if (it2 != test1.end())
{
cout<<"find it:"<<it2->first<<"---"<<it2->second<<endl;
}
else
{
cout<<"not find it"<<endl;
}

5、删除元素

    //删除元素
it2 = test1.find();
test1.erase(it2);

这些操作基本上都和set的差不多,好多函数一模一样。

  

STL学习笔记5--map and multimap的更多相关文章

  1. STL学习笔记— —容器map和multimap

    简单介绍 在头文件<map> 中定义 namespace std { template <typename Key, typename T, typename Compare = l ...

  2. Effective STL 学习笔记: Item 22 ~ 24

    Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...

  3. Effective STL 学习笔记 39 ~ 41

    Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  4. Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value

    Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value */--> div.org-src-container ...

  5. Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据

    Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...

  6. Effective STL 学习笔记 32 ~ 33

    Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  7. Effective STL 学习笔记 31:排序算法

    Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  8. Effective STL 学习笔记 Item 30: 保证目标区间足够大

    Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...

  9. Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor

    Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor */--> div ...

  10. Effective STL 学习笔记 Item 21:Comparison Function 相关

    Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...

随机推荐

  1. uvm_svcmd_dpi——DPI在UVM中的实现(二)

    UVM中有需要从cmmand line 输入参数的需求,所有uvm_svcmd_dpi.svh和uvm_svcmd_dpi.cc 文件就是实现功能. uvm_svcmd_dpi.svh的源代码如下,我 ...

  2. zabbix-2.2.2(Ubuntu 14.04 LTS/OpenLogic 7.2)

    平台: arm 类型: ARM 模板 软件包: apache-2.4.7 mariadb-5.5.50 mysql-5.5.52-0ubuntu0.14.04.1 php-5.4.16 php-5.5 ...

  3. .NET 前台调用后台事件和方法实现小结

    转自:https://www.cnblogs.com/kinger906/p/3431842.html 除了下文讲的方式外,还有一种方式:html里面使用ajax写好提交方式和提交参数,然后以写一行带 ...

  4. .NET中异常类(Exception)

    异常:程序在运行期间发生的错误.异常对象就是封装这些错误的对象. try{}catch{}是非常重要的,捕获try程序块中所有发生的异常,如果没有捕获异常的话,程序运行的线程将会挂掉,更严重的是这些错 ...

  5. xp_delete_files不起作用解决方法

    xp_delete_file用来删除数据库的备份文件和维护计划文本报告.示例: ,N'D:\Backup\Diff',N'bak',N'2019-05-29T10:03:41' 第一个参数表示文件类型 ...

  6. 解决“SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/OpenDatasource' 的访问……”【转】

    SQL Server 阻止了对组件 /'Ad Hoc Distributed Queries/' 的访问 在Sql Server中查询一下Excel文件的时候出现问题: SELECT *  FROM ...

  7. HDU 2602 Bone Collector骨头收藏者(01背包)

    题意:收藏骨头. 思路: 常规的01背包. #include <iostream> #define N 1005 using namespace std; int volume[N]; / ...

  8. linux 命令——51 lsof(转)

    lsof(list open files) 是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以 如传输控制 ...

  9. POJ 3050 Hopscotch(dfs,stl)

    用stack保存数字,set判重.dfs一遍就好.(或者编码成int,快排+unique #include<cstdio> #include<iostream> #includ ...

  10. NOIP2018提高组Day1 解题报告

    前言 关于\(NOIP2018\),详见此博客:NOIP2018学军中学游记(11.09~11.11). 这次\(NOIP\ Day1\)的题目听说很简单(毕竟是三道原题),然而我\(T3\)依然悲剧 ...