浅析map和multimap应用的小程序

创建类如下:

class Person
{
private:
    char *name;
    int age;
    friend class lesserAge;
public:
    Person(const char *ch = NULL, int num = 0):age(num){  //1
        name = new char[strlen(ch)+1];
        strcpy(name, ch);
        name[strlen(ch)] = '\0';
        //cout << "name:" << name << endl;
    }
    Person(const Person &ps){  //2
        name = new char[strlen(ps.name)+1];
        strcpy(name,ps.name);
        name[strlen(ps.name)] = '\0';
        age = ps.age;
    }
    ~Person(){  //3
        delete[] name;
    }
    bool operator<(const Person &p) const {
        return strcmp(name, p.name) < 0;
    }
    friend ostream &operator<<(ostream &out, const Person &ps){  //4
        out << "(" << ps.name << ", " << ps.age << ")";
        return out;
    }
};
class lesserAge
{
public:
    int operator() (const Person &p1, const Person &p2) const {
        return p1.age < p2.age;
    }
};
  1. 构造函数:赋值的时候可能输入的值不完整,所以设定默认值,对于char *指针赋值的时候,要采用复制的方式,否则会造成指针指向同一块区域,在析构函数起作用的时候会形成野指针。
  2. 这里要显示写出复制构造函数的原因一致,在用类对象构建其他对象的时候,如果采用隐式构造函数,会导致和上述一样的问题。
  3. 析构函数,在有内存申请的时候,老样子^_^
  4. 重载<<为了接下来打印使用。
    main函数里结构如下:
pair<Person, char *> p[] = {
        pair<Person, char *>(Person("Gregg", 25), "Pittsburgh"),
        pair<Person, char *>(Person("Ann", 30), "Boston"),
        pair<Person, char *>(Person("Bill", 20), "Belmont")};
    map<Person, char *> cities(p, p+3);  //1
    map<Person, char *>::iterator it  = cities.begin();
    cities[Person("Kay", 40)] = "New York";  //2
    cities["Jenny"] = "Newark";  //3
    cities.insert(map<Person, char *>::value_type(Person("Kay", 40), "Detroit"));  //4
    cities.insert(pair<Person, char *>(Person("Kay", 40), "Austin"));
    map<Person, char *>::iterator i;
    for(i = cities.begin(); i != cities.end(); i++)
        cout << (*i).first << ' ' << (*i).second << endl;
  1. 构造map的方式之一,不多介绍
  2. 以Person类型的对象为索引(可以看作广义上的数组)
  3. 对应上面的构造函数,age为默认值0,此处意为在索引Person(“Jenny”, 0)处将内容置为”Newark”。
  4. 以map< T, U >::value_type的类型构造对象

完整代码如下:

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

class Person
{
private:
    char *name;
    int age;
    friend class lesserAge;
public:
    Person(const char *ch = NULL, int num = 0):age(num){
        name = new char[strlen(ch)+1];
        strcpy(name, ch);
        name[strlen(ch)] = '\0';
        //cout << "name:" << name << endl;
    }
    Person(const Person &ps){
        name = new char[strlen(ps.name)+1];
        strcpy(name,ps.name);
        name[strlen(ps.name)] = '\0';
        age = ps.age;
    }
    ~Person(){
        delete[] name;
    }
    bool operator<(const Person &p) const {
        return strcmp(name, p.name) < 0;
    }
    friend ostream &operator<<(ostream &out, const Person &ps){
        out << "(" << ps.name << ", " << ps.age << ")";
        return out;
    }
};

class lesserAge
{
public:
    int operator() (const Person &p1, const Person &p2) const {
        return p1.age < p2.age;
    }
};

int main()
{
    pair<Person, char *> p[] = {
        pair<Person, char *>(Person("Gregg", 25), "Pittsburgh"),
        pair<Person, char *>(Person("Ann", 30), "Boston"),
        pair<Person, char *>(Person("Bill", 20), "Belmont")};
    map<Person, char *> cities(p, p+3);
    map<Person, char *>::iterator it  = cities.begin();
    cities[Person("Kay", 40)] = "New York";
    cities["Jenny"] = "Newark";
    cities.insert(map<Person, char *>::value_type(Person("Kay", 40), "Detroit"));
    cities.insert(pair<Person, char *>(Person("Kay", 40), "Austin"));
    map<Person, char *>::iterator i;
    for(i = cities.begin(); i != cities.end(); i++)
        cout << (*i).first << ' ' << (*i).second << endl;
    cout << "-------------------------------------" << endl;
    cities[p[1].first] = "Chicago";
    multimap<Person, char *> mCities(p, p+3);
    mCities.insert(pair<Person, char *>(Person("Kay", 40), "Austin"));
    mCities.insert(pair<Person, char *>(Person("Kay", 40), "Austin"));
    mCities.insert(pair<Person, char *>(Person("Kay", 40), "Detroit"));
    multimap<Person, char *>::iterator mi;
    for(mi = mCities.begin(); mi != mCities.end(); mi++)
        cout << mi->first << ' ' << mi->second << endl;
    cout << "-------------------------------------" << endl;
    (*(mCities.find(Person("Kay", 40)))).second = "New York";
    for(mi = mCities.begin(); mi != mCities.end(); mi++)
        cout << mi->first << ' ' << mi->second << endl;

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值