参考链接:http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html

map头文件

#include <map>

map添加数据;

map<int ,string> maplive;  
   1.maplive.insert(pair<int,string>(102,"aclive"));
   2.maplive.insert(map<int,string>::value_type(321,"hai"));
   3, maplive[112]="April";//map中最简单最常用的插入添加!

map中元素的查找:

find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。

map<int ,string >::iterator l_it;; 
   l_it=maplive.find(112);
   if(l_it==maplive.end())
                cout<<"we do not find 112"<<endl;
   else cout<<"wo find 112"<<endl;

map中元素的删除:
   如果删除112;
   map<int ,string >::iterator l_it;;
   l_it=maplive.find(112);
   if(l_it==maplive.end())
        cout<<"we do not find 112"<<endl;
   else  maplive.erase(l_it);  //delete 112;

map的sort问题:
  Map中的元素是自动按key升序排序,所以不能对map用sort函数:

map的基本操作函数:
      C++ Maps是一种关联式容器,包含“关键字/值”对
      begin()          返回指向map头部的迭代器
      clear()         删除所有元素
      count()          返回指定元素出现的次数
      empty()          如果map为空则返回true
      end()            返回指向map末尾的迭代器
      equal_range()    返回特殊条目的迭代器对
      erase()          删除一个元素
      find()           查找一个元素
      get_allocator()  返回map的配置器
      insert()         插入元素
      key_comp()       返回比较元素key的函数
      lower_bound()    返回键值>=给定元素的第一个位置
      max_size()       返回可以容纳的最大元素个数
      rbegin()         返回一个指向map尾部的逆向迭代器
      rend()           返回一个指向map头部的逆向迭代器
      size()           返回map中元素的个数
      swap()            交换两个map
      upper_bound()     返回键值>给定元素的第一个位置
      value_comp()      返回比较元素value的函数

stl map容器中指针的释放

struct MyStruct
{
    int i;
    char c;

MyStruct(int i, char c)
    {
        this->i = i;
        this->c = c;
    }
};

int stl_ptr_test()
{
    MyStruct* pst1 = new MyStruct(1, 'a');
    MyStruct* pst2 = new MyStruct(1, 'a');
    MyStruct* pst3 = new MyStruct(3, 'c');

map<int, MyStruct*> map1; // 其中的value为指针型的

map1.insert(pair<int, MyStruct*>(10, pst1));
    map1.insert(pair<int, MyStruct*>(11, pst2));
    map1.insert(pair<int, MyStruct*>(12, pst3));

map<int, MyStruct*>::iterator iter = map1.find(11);
    cout << iter->second->i << ' ' << iter->second->c << endl;

MyStruct* pst = iter->second;
    pst->c = 'b';

iter = map1.find(11);
    cout << iter->second->i << ' ' << iter->second->c << endl;

iter = map1.find(10);
    delete iter->second; // 释放指针
    map1.erase(10); // 从map中删除元素

iter = map1.find(10);
    if (iter == map1.end())
    {
        cout << "not found" << endl;
    }
    else
    {
        cout << "found: " << iter->second->i << ' ' << iter->second->c << endl;
    }

for (iter = map1.begin(); iter != map1.end();)
    {
        cout << iter->second->i << " " << iter->second->c << endl;

        delete iter->second; // 释放指针
        map1.erase(iter++); // 从map中删除元素,注意iter++的写法
    }

map1.clear();

return 0;
}

.//在遍历时删除

int main(int argc, char* argv[])
{
map<string, string> mapData; mapData["a"] = "aaa";
mapData["b"] = "bbb";
mapData["c"] = "ccc"; for (map<string, string>::iterator i=mapData.begin(); i!=mapData.end(); /*i++*/)
{
if (i->first == "b")
{
mapData.erase(i++);
}
else
{
i++;
}
}
return ;
}

[STL][C++]MAP的更多相关文章

  1. C++ STL中Map的按Key排序和按Value排序

    map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区 分),我们用map来进 ...

  2. STL中map与hash_map的比较

    1. map : C++的STL中map是使用树来做查找算法; 时间复杂度:O(log2N) 2. hash_map : 使用hash表来排列配对,hash表是使用关键字来计算表位置; 时间复杂度:O ...

  3. STL中map,set的基本用法示例

    本文主要是使用了STL中德map和set两个容器,使用了它们本身的一些功能函数(包括迭代器),介绍了它们的基本使用方式,是一个使用熟悉的过程. map的基本使用: #include "std ...

  4. STL中map与hash_map容器的选择收藏

    这篇文章来自我今天碰到的一个问题,一个朋友问我使用map和hash_map的效率问题,虽然我也了解一些,但是我不敢直接告诉朋友,因为我怕我说错了,通过我查询一些帖子,我这里做一个总结!内容分别来自al ...

  5. C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET

    C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET C++ STL中Map的相关排序操作:按Key排序和按Value排序 分类: C ...

  6. STL之map排序

    描述 STL的map中存储了字符串以及对应出现的次数,请分别根据字符串顺序从小到大排序和出现次数从小到大排序. 部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码. int main() { ...

  7. C++中的STL中map用法详解(转)

    原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解   Map是STL的一个关联容器,它提供 ...

  8. C++ STL中Map的按Key排序跟按Value排序

    C++ STL中Map的按Key排序和按Value排序 map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定 ...

  9. [STL] Implement "map", "set"

    练习热身 Ref: STL中map的数据结构 C++ STL中标准关联容器set, multiset, map, multimap内部采用的就是一种非常高效的平衡检索二叉树:红黑树,也成为RB树(Re ...

  10. stl中map的四种插入方法总结

    stl中map的四种插入方法总结方法一:pair例:map<int, string> mp;mp.insert(pair<int,string>(1,"aaaaa&q ...

随机推荐

  1. 布置theano(Ubuntu14.04 LTS)

    引言 由于研究生阶段将会从事自然语言处理方向的研究,目前要用到机器学习和深度学习相关的框架,那应老师的要求,将要使用theano,由于theano官方文档中关于ubuntu下配置的问题并没有给出很好的 ...

  2. 使用Universal USB Installer安装Ubuntu

    1.下载Universal USB Installer 下载地址: 2.下载ubuntu 14 desktop.iso 运行Universal USB Installer,找到电脑上 ubuntu 1 ...

  3. Summary: Trie Data Structure

    Implement a Trie Data Structure, and search() & insert() function: we need to implement both Cla ...

  4. 使用visual studio 2012 编译opencv2.4.9

    最近,由于需要从opencv源码部分对opencv中的某个函数进行修改,以提升算法的速度,因此一直在尝试使用vs2012来编译opencv.期间不乏多次的失败.今天通过实验发现了自己编译的opencv ...

  5. UVa10023手动开大数平方算法

    题目链接:UVa 10023 import java.math.BigInteger; import java.util.Scanner; public class Main { public sta ...

  6. acm pc^2的配置与使用

    -------------------------------------------------------------------------------------- !!! 转载请注明: 转自 ...

  7. [摘录]quarts :overview

    最近项目用了quarts 这个 任务调度器.今天开始详细的学习学习.参考链接:http://www.quartz-scheduler.org/# Quartz Enterprise Job Sched ...

  8. C++之路进阶——bzoj1468(tree)

    F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser  gryz2016 Logout 捐赠本站 Notice:由于本OJ ...

  9. yii框架中保存第三方登录信息

    (第三方登录) 创建应用,域名,详情请看:http://www.cnblogs.com/xujn/p/5287157.html 效果图:

  10. jq 换图片路径

    $("#index_01")[0].src="images/index_01_1.jpg"; //更改ID为index_01的图片的src值 $("# ...