C++ map操作

maps.cpp

#include "maps.h"
#include "map"
#include <string>
#include <iostream>
#include "student.h"

using namespace std;

class sort {
public:
    bool operator()(Student const &_A, Student const &_B) const {
        if (_A.num < _B.num)
            return true;
        if (_A.num > _B.num)
            return false;
        if (_A.age < _B.age)
            return true;
        if (_A.age > _B.age)
            return false;
        if (_A.name.compare(_B.name) < 0)
            return true;
        if (_A.name.compare(_B.name) > 0)
            return false;
        return false;
    }
};

void showmMaps() {
    map<int, string> mapis_1;
    map<int, Student> mapis_2;
    map<int, char> maps_3;
    map<string, char> maps_4;
    //这个构造为仿函数  类似于java的Map的compareable
    map<Student, int, sort> mapStudent;
    mapStudent.insert(pair<Student, int>(Student("maqi", 1102, man, 18), 1));
    mapStudent.insert(pair<Student, int>(Student("lisi", 1102, man, 19), 2));
    mapStudent.insert(pair<Student, int>(Student("wanger", 1104, man, 28), 3));
    mapStudent.insert(pair<Student, int>(Student("maqi", 1111, wuman, 18), 4));
    mapStudent.insert(pair<Student, int>(Student("maqi(WH)", 1111, wuman, 18), 4));
    //add方法
    mapis_1[1002] = string("maqi_1002");
    mapis_1[1001] = string("maqi_1001");
    mapis_1.insert(pair<int, string>(1003, "maqi_1003"));
    mapis_2.insert(pair<int, Student>(1, Student("maqi_1")));
    mapis_2.insert(pair<int, Student>(2, Student("maqi_2")));
    mapis_2.insert(pair<int, Student>(3, Student("maqi_3")));
    const pair<map<int, string>::iterator, bool> &pair = mapis_1.insert(
            map<int, string>::value_type(1005, "maqi_1005"));
    if (pair.second == true)cout << "insert success?  " << true << endl;
    map<int, string> mapis_copy(mapis_1.begin(), mapis_1.end());
    cout << "mapis_1.size() " << mapis_1.size() << endl;
    cout << "mapis_1.count(1002) " << mapis_1.count(1002) << endl;

    map<int, string>::iterator it;
    //动态数组 for循环
    for (it = mapis_1.begin(); it != mapis_1.end(); ++it) {
        cout << it->first << ' ' << it->second << endl;
    }
    cout << "===================================================" << endl;
    for (it = mapis_copy.begin(); it != mapis_copy.end(); ++it) {
        cout << it->first << ' ' << it->second << endl;
    }

    cout << "==============应用反相迭代器==============" << endl;
    map<int, string>::reverse_iterator iter;
    for (iter = mapis_1.rbegin(); iter != mapis_1.rend(); iter++)
        cout << iter->first << "  " << iter->second << endl;

    cout << "=================find 用法====================" << endl;
    map<int, string>::iterator finditer = mapis_1.find(1006);
    if (finditer != mapis_1.end())
        cout << "Find, the value is " << iter->second << endl;
    else
        cout << "Do not Find" << endl;

    cout << "=================erase  用法====================" << endl;
    mapis_1.erase(1001);
    mapis_1.erase(mapis_1.find(1005));
    //迭代器循环while
    map<int, string>::iterator mapit = mapis_1.begin();
    while (mapit != mapis_1.end()) {
        cout << mapit->first << ' ' << mapit->second << endl;
        mapit++;
    }
    cout << "=================sort  用法====================" << endl;
    //int 默认支持 < 操作
    for (auto item : mapis_2) {
        cout << item.first << ' ' << item.second.name << endl;
    }
    cout << "=================仿函数 用法====================" << endl;
    for (auto item : mapStudent) {
        cout << item.second << ' ';
        cout << " name = " << item.first.name << " num = " << item.first.num << " sex = "
             << item.first.sex << " age = " << item.first.age
             << endl;
    }
    //清空
    mapis_1.clear();
    cout << "mapis_1.empty()" << mapis_1.empty() << endl;
}

总结:
1.map也可以迭代,并没像java那样分那么清楚,迭代方法和list,vector相似。
2.反向迭代reverse_iterator 了解一下。
3.仿函数方便结构体或class的遍历,注意map的写法。

 map<Student, int, sort> mapStudent;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值