#include<iostream>
#include<map>
#include<string> #define s second
#define f first using namespace std;
// map 的构造
map<int , int> ii;
map<string , string> ss;
map<string , int> si;
typedef map<int , string> mapType;
mapType is; int main()
{
// map 的插入方法 1 用insert函数插入value_type数据
ii.insert(map<int, int >::value_type(,));
ii.insert(map<int , int >::value_type(,));
ii.insert(map<int , int >::value_type(,));
cout << "ii : " << endl << ii.size() << endl;
for(map<int , int>::iterator it = ii.begin(); it != ii.end(); it++)
cout << "key : " << it->f << " value : " << it->s << endl;
// map 的插入方法 2 用insert函数插入pair数据
ss.insert(pair<string, string>("a", "a"));
ss.insert(pair<string, string>("b", "b"));
ss.insert(pair<string, string>("c", "c"));
cout << "ss : " << endl << ii.size() << endl;
for(map<string , string>::iterator it = ss.begin(); it != ss.end(); it++)
cout << "key : " << it->f << " value : " << it->s << endl;
// map 的插入方法 3 用insert函数插入make_pair数据
si.insert(make_pair<string, int>("a",));
si.insert(make_pair<string, int>("b",));
si.insert(make_pair<string, int>("c",));
cout << "si : " << endl << si.size() << endl;
for(map<string , int>::iterator it = si.begin(); it != si.end(); it++)
cout << "key : " << it->f << " value : " << it->s << endl;
// map 的插入方法 4 用数组插入
ii[] = ;
ii[] = ;
ii[] = ;
ii[] = ;
cout << "ii : " << endl << ii.size() << endl;
for(map<int , int>::iterator it = ii.begin(); it != ii.end(); it++)
cout << "key : " << it->f << " value : " << it->s << endl;
// 前三种方法后面的值不会覆盖前面的值
// 查找方法 1
// count函数求的是关键字key的个数?key是不能重复的,所以返回只有0和1两种结果。
cout << "查找关键字1的结果 : " << ii.count() << endl;
cout << "查找关键字1的结果 : " << ii.count() << endl;
cout << "查找关键字a的结果 : " << ss.count("a") << endl;
cout << "查找关键字a的结果 : " << si.count("c") << endl;
//查找方法 2
map<string, int>::iterator it;
it = si.find("c");
if(it != si.end())
cout << "find it : " << it->s << endl;
else
cout << "not find" << endl; }

map 的用法的更多相关文章

  1. c++map的用法 分类: POJ 2015-06-19 18:36 11人阅读 评论(0) 收藏

    c++map的用法 分类: 资料 2012-11-14 21:26 10573人阅读 评论(0) 收藏 举报 最全的c++map的用法 此文是复制来的0.0 1. map最基本的构造函数: map&l ...

  2. STL中map的用法

    map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候 ...

  3. C++11中map的用法

    最全的c++map的用法 1. map最基本的构造函数:map<string ,int>mapstring; map<int,string >mapint;map<sri ...

  4. entrySet用法 以及遍历map的用法

    entrySet用法 以及遍历map的用法   keySet是键的集合,Set里面的类型即key的类型entrySet是 键-值 对的集合,Set里面的类型是Map.Entry   1.keySet( ...

  5. python map 常见用法

    python map 常见用法2017年02月01日 19:32:41 淇怪君 阅读数:548版权声明:欢迎转载,转载请注明出处 https://blog.csdn.net/Tifficial/art ...

  6. 8 map的用法

    what's map go里面的map和python字典差不多. 类似其他语言中的哈希表或者字典,以key-value的形式存储的数据 key必须是支持==或者!=比较运算的类型,不可以是函数.map ...

  7. forEach、for+i、map的用法及区别

      array.forEach(callback[, thisObject]); 下面是参数的详细信息: 1. callback : 函数测试数组的每个元素. 2.thisObject : 对象作为该 ...

  8. set/multiset和map/multimap用法小结

    二叉搜索树是ACM中经常需要用到的数据结构,熟练掌握map和set的用法很关键,现对其做一个简单的总结. 主要的功能有:插入元素,查找元素,删除,遍历/反向遍历. 插入,删除和查找操作的时间都和树的高 ...

  9. STL 之 map的用法

    Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候 ...

  10. js数组中foEach和map的用法详解 jq中的$.each和$.map

    数组中foEach和map的用法详解 相同点: 1.都是循环遍历数组(仅仅是数组)中的每一项. 2.forEach() 和 map() 里面每一次执行匿名函数都支持3个参数:数组中的当前项value, ...

随机推荐

  1. LeetCode 905 Sort Array By Parity 解题报告

    题目要求 Given an array A of non-negative integers, return an array consisting of all the even elements ...

  2. java 线程(四)线程安全 同步方法

    package cn.sasa.demo2; import java.util.concurrent.ExecutionException; public class ThreadDemo { pub ...

  3. 10.1-uC/OS-III任务堆栈空间

    1.设置任务优先级 嵌入式系统中的重要的应用应该被设置为高优先级,一些显示操作就应该被设置为低优先级. 然而, 由于实时系统的复杂性, 在大多数情况下任务的优先级是不能被事先确定的.多数系统中,不是所 ...

  4. RN全局的变量,方法,全局类,全局类方法

    为了方便学习,很简单的小Demo,不懂可以下方留言,百分百原创,相互学习,相互进步 全局的方法 创建一个js文件,命名OvallAll //全局的方法 //这里export default 只能输出一 ...

  5. 萌新接触前端的第一课——HTML

    HTML web服务本质(好吧这个先不用知道也可以) import socket def main(): sock = socket.socket(socket.AF_INET, socket.SOC ...

  6. 正交表和TCG的使用

    正交表法是一种有效减少测试用例个数的设计方法. 正交表法的依据是Galois理论,从大量的实验数据中挑选适量的.有代表性的点,从而合理的安排实验的一种科学实验设计方法.在测试用例的设计中,可以从大量的 ...

  7. ROS安装(2)

    1.Ubuntu无法启动了,进入不了启动界面 刚高兴一会会儿,马上系统重启,进不了登陆界面了.只有控制台的login,无语.试了好几种方法都没有解决. 重装吧!重装Ubuntu之后,装完ROS Kin ...

  8. function module 之间调用

    1: 在一个function group 中定义一个function module 2:在另外一个module中调用该module "调用其它function 要用 单引号 引着. 一个mo ...

  9. Scala之隐式转换

    概述 简单说,隐式转换就是:当Scala编译器进行类型匹配时,如果找不到合适的候选,那么隐式转化提供了另外一种途径来告诉编译器如何将当前的类型转换成预期类型. 隐式转换有四种常见的使用场景: 将某一类 ...

  10. Ubuntu Server16.04 配置网卡

    展示全部启动网卡 $ ifconfig 配置网卡 $ sudo vi /etc/network/interfaces auto enp2s0 iface enp2s0 inet static addr ...