• STL map与multimap


    1.map(映射)、multimap(多映射)

    2.红黑树(数据结构)

    3.1)insert:4种方法

     2)count和find

     3)erase:3种方法

    注意:不能通过find进行修改!

    #include<iostream>
    #include<map>
    #include<string>
    
    using namespace std;
    int map_function() {
        //这个map是用二叉树结构做的,二叉树中功能最强大的红黑树
        //因为是红黑树,所以map容器中进行查找数据的时候也是非常的快的。
        //map是非常大的一个容器是自动进行增长的
        //map这个里面有自动去重的功能
    
        map <int,string> a;
        //这个multimap可以插入重复的键值对
        multimap<int, string>ma;
        map<string, int>分数;
    
        a.insert(map<int, string>::value_type(1, "one"));
        a.insert(map<int,string>::value_type(2, "two"));
        a.insert(map<int, string>::value_type(3, "three"));
        a.insert(make_pair(-1, "Minus One"));
        a.insert(pair<int, string>(1000, "One Thousand"));
      //插入的后面这个显示不出来。 a.insert(pair
    <int, string>(1000, "One Thousandsss")); a[1000000] = "One Million"; a.insert(map<int, string>::value_type(1, "ones")); 分数.insert(make_pair("张飞", 99)); 分数.insert(make_pair("刘备", 56)); 分数["关羽"] = 87l; cout << "最简单的查找: " << endl; cout << 分数["刘备"] << endl; cout << a[3] << endl; cout << "map 里面一共有" << a.size() << "个key-value对数据" << endl; map<int, string>::const_iterator i; for (i = a.begin(); i!=a.end(); ++i) { cout << "key" << i->first<<" "; cout << "value" <<" "<< i->second.c_str(); cout << endl; } ma.insert(multimap<int, string>::value_type(3, "Three")); ma.insert(multimap<int, string>::value_type(45, "Forty Five")); ma.insert(make_pair(-1, "Minux One")); ma.insert(pair<int, string>(1000, "One Thousand")); ma.insert(pair<int, string>(1000, "One Thousandss")); ma.insert(pair<int, string>(1001, "One Thousand")); ma.insert(pair<int, string>(1001, "One Thousandss")); cout << endl << "multimap里面有" << ma.size() << "个数据." << endl; multimap<int, string>::const_iterator it; for (it = ma.begin(); it != ma.end(); ++it) { cout <<"key"<< it->first << " "; cout << "value"<<it->second << endl; } //进行查找 //cout << "multimap里面有" << ma.count(1000) << "个1000" << endl; //multimap<int, string>::iterator fi; //fi = ma.find(1000); //if (fi != ma.end()) //{ // cout << "找到了1000" << endl; // size_t n = ma.count(1000); // cout << "一共有" << n << "个1000" << endl; // for (size_t i = 0; i < n; ++i) { // cout << "key" << fi->first; // cout << "value" << fi->second << endl; // ++fi; // } // //cout << "找到了: " << fi->first << "=" << fi->second.c_str() << endl; //} //else //{ // cout << "没有找到" << endl; //} //进行删除操作 if (ma.erase(-1) > 0) cout << endl << "删除-1成功!" << endl; multimap<int, string>::iterator iElementFound = ma.find(45); if (iElementFound != ma.end()) { ma.erase(iElementFound); cout << "删除45成功" << endl; } cout<<"删除了"<< ma.erase(1000)<<"个1000"<<endl; multimap<int, string>::iterator itt; itt = ma.find(1000); if(itt!=ma.end()) { cout << "找到了1000" << endl; } else { cout << "没有找到1000" << endl; } ma.erase(ma.lower_bound(1001), ma.upper_bound(1001)); multimap<int, string>::iterator ittt; ittt = ma.find(1001); if (ittt != ma.end()) { cout << "找到了1001" << endl; } else { cout << "没有找到1001" << endl; } return 0; } int main() { map_function(); }
  • 相关阅读:
    为蓝桥杯准备
    Java里子类调用父类构造方法问题
    boost库的Singleton的实现以及static成员的初始化问题
    VS2005调试小记
    <转载>程序员每天该做的事
    vs2005, 2008 使用了未初始化的msg变量
    转载 vs2005 快捷键大全
    VS2005右键点击转到定义后出现“未定义符号”的提示及其解决
    软件工程配置规范(VC2005) 第二版(转载)
    匆忙赶路的时候别忘了适时停下来回头看看
  • 原文地址:https://www.cnblogs.com/littleswan/p/12730842.html
Copyright © 2020-2023  润新知