• map小练


     1 //
     2 //map是一个标准的关联式容器,一个map是一个键值对序列,即(key,value)对。
     3 //它提供基与key 的快速检索能力。map中key值是唯一的。
     4 //集合中的元素按一定的顺序排列。元素插入过程是按排列规则插入,所以不能指定插入位置。
     5 //map具体实现采用红黑树变体的平衡二叉树的数据结构。在插入操作和删除操作上比vector快
     6 //map可以直接存取key值所对应的value支持[]操作,如map[key]value
     7 //
     8 //
     9 //pair类型
    10 //pair是标准库类型,定义在头文件utility中
    11 //一个pair保存两个数据成员。类似容器,pair是一个用来生成特定类型的模版。
    12 //当创建一个pair时,我们必须提供两个类型名,pair的数据类型将具有对应的类型。
    13 //两个类型不要求一样
    14 //pair<string, string> anon;      //保存两个string,两个空字符串
    15 //pair<string, size_t> word_count;//保存一个string和一个size_t
    16 //pair<string, vector<int>> line;//保存string和vector<int>
    17 #include "stdafx.h"
    18 #include<iostream>
    19 using namespace std;
    20 #include<map>//使用map之前,必须先包含头文件
    21 int _tmain(int argc, _TCHAR* argv[])
    22 {
    23     map<int, char> mapA;
    24     map<string, float> mapB;
    25     map<string, size_t> word_count;
    26     //在map中插入与修改元素
    27     //通过构造临时对象插入对象
    28     word_count.insert({ "word", 1 });
    29     //通过make_pair返回一个pair对象的方式插入对象
    30     word_count.insert(make_pair("word", 1));
    31     //通过pair方式插入对象
    32     word_count.insert(pair<string, size_t>("word", 1));
    33     //通过value_type的方式插入对象
    34     word_count.insert(map<string, size_t>::value_type("word", 1));
    35     //通过数组下表访问对象
    36     word_count["word"] = 1;
    37     //在map中删除元素
    38     //根据传入的单词删除指定元素
    39     word_count.erase("word");
    40     //删除map中最开始的元素(传入迭代器)
    41     word_count.erase(word_count.begin());
    42     return 0;
    43 }
    让数据变得更安全!
  • 相关阅读:
    使用PuTTY时的文件上传下载方法
    [emacs org-mode小技巧] org-indent-mode 让文档更容易阅读
    tldr 的安卓客户端
    如何配置ssh免密码登录
    降低屏幕亮度,减缓眼疲劳 (linux/windows/firefox/android)
    Android上面安装Linux的方法
    Spring中argNames的含义
    js 输出 select 的值
    js 优先级
    layer.confirm( 找不到 on函数
  • 原文地址:https://www.cnblogs.com/Alyoyojie/p/5146406.html
Copyright © 2020-2023  润新知