算法之旅 直奔map

来源:互联网 发布:ui设计师老被叫美工 编辑:程序博客网 时间:2024/05/14 03:55

小学map

  • 真言

继续吸收养分;

  • 总结

map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。对于迭代器来说,可以修改实值,而不能修改key。

  • 知识
    1. 自动建立Key - value的对应。key 和 value可以是任意你需要的类型。
    2. 根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。
    3. 快速插入Key - Value 记录。
    4. 快速删除记录
    5. 根据Key 修改value记录。
    6. 遍历所有记录。
  • 代码

test.cpp
#include<map>#include<iterator>#include<iostream>#include<string>using namespace std;int main(){map<string,int>  word ;word.insert(map<string,int>::value_type("one",1));map<string,int>::iterator it = word.begin();while(it != word.end()){cout<<(*it).first<<" "<<(*it).second<<endl;it++;}word.erase(word.begin(),word.end());system("pause");return 0;}



0 0
原创粉丝点击