C++ STL map

常用的map一些操作在下面的程序中都可以找到,相信你一定会看懂的。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
using namespace std;

map<string, int> mp;
map<string, int> ::iterator it;
/*     map<string,int>::iterator it;
 *     定义一个迭代指针it.
 *     it->first 为索引键值,it->second 为值。
 */

void show()
{
    cout << "---------------------" << endl;
    cout << "size = " << mp.size() << endl; //  map的大小
    for(it = mp.begin(); it != mp.end(); it ++)
        cout << "key = " << it->first << "    " << "value = " << it->second << endl;
}

void Find(string m)
{
    it = mp.find(m);
    /*     用find函数来定位数据出现位置,它返回的一个迭代器,
     *     当数据出现时,它返回数据所在位置的迭代器,
     *     如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器
     */
    if(it == mp.end())
    {
        cout << "sorry," << m << " not find" << endl;
        return ;
    }
    else
    {
        cout << it -> first << " finded" << ", it`s value is " << it->second << endl;
        return ;
    }
}
int main()
{
    mp["111"] = 0;
    mp["222"] = 2;
    mp["333"] = 3;
    mp["111"] = 1;  //  有效
    show();
    mp.insert(make_pair("444", 4)); //  插入
    mp.insert(make_pair("111", 11)); // 无效的插入
    mp.insert(make_pair("555", 5));
    show();
    string x = "555", y = "666";
    Find(x), Find(y);
    mp.erase("111");    // 用关键字删除
    show();
    it = mp.find("222");
    mp.erase(it);    // 用迭代器删除
    show();
    mp.erase(mp.begin());
    show();
    mp.erase(mp.begin(), mp.end());     // 用迭代器,成片的删除
    show();
    mp["999"] = 9;
    show();
    if(!mp.empty()) // 判定map中是否有数据可以用empty()函数,它返回true则说明是空map
        mp.clear(); // 清空map中的数据可以用clear()函数,
    show();
    return 0;
}
【以下部分为摘抄】
排序
这里要讲的是一点比较高深的用法了,排序问题,STL中默认是采用小于号来排序的,
以上代码在排序上是不存在任何问题的,因为上面的关键字是int型,它本身支持小于号运算,
在一些特殊情况,比如关键字是一个结构体,涉及到排序就会出现问题,因为它没有小于号操作,
insert等函数在编译的时候过不去,下面给出两个方法解决这个问题
第一种:小于号重载,程序举例

#include <map>
#include <string>
using namespace std;
typedef struct tagStudentInfo
{
    int nID;
    string strName;
} StudentInfo, *PStudentInfo; //学生信息
int main()
{
    int nSize;
    //用学生信息映射分数
    map<StudentInfo, int>mapStudent;
    map<StudentInfo, int>::iterator iter;
    StudentInfo studentInfo;
    studentInfo.nID = 1;
    studentInfo.strName = “student_one”;
    mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));
    studentInfo.nID = 2;
    studentInfo.strName = “student_two”;
    mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));
    for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)
        cout<<iter->first.nID<<endl<<iter->first.strName<<endl<<iter->second<<endl;
}

/*以上程序是无法编译通过的,只要重载小于号,就OK了,如下:*/

typedef struct tagStudentInfo
{
    int nID;
    string strName;
    bool operator < (tagStudentInfo const& _A) const
    {
        //这个函数指定排序策略,按nID排序,如果nID相等的话,按strName排序
        if(nID < _A.nID)  return true;
        if(nID == _A.nID) return strName.compare(_A.strName) < 0;
        return false;
    }
} StudentInfo, *PStudentInfo; //学生信息
第二种:仿函数的应用,这个时候结构体中没有直接的小于号重载,程序说明

#include <map>
#include <string>
using namespace std;
typedef struct tagStudentInfo
{
    int nID;
    string strName;
} StudentInfo, *PStudentInfo; //学生信息

class sort
{
public:
    bool operator() (StudentInfo const &_A, StudentInfo const &_B) const
    {
        if(_A.nID < _B.nID) return true;
        if(_A.nID == _B.nID) return _A.strName.compare(_B.strName) < 0;
        return false;
    }
};

int main()
{
    //用学生信息映射分数
    map<StudentInfo, int, sort>mapStudent;
    StudentInfo studentInfo;
    studentInfo.nID = 1;
    studentInfo.strName = “student_one”;
    mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));
    studentInfo.nID = 2;
    studentInfo.strName = “student_two”;
    mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值