创建类如下:
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;
}
};
- 构造函数:赋值的时候可能输入的值不完整,所以设定默认值,对于char *指针赋值的时候,要采用复制的方式,否则会造成指针指向同一块区域,在析构函数起作用的时候会形成野指针。
- 这里要显示写出复制构造函数的原因一致,在用类对象构建其他对象的时候,如果采用隐式构造函数,会导致和上述一样的问题。
- 析构函数,在有内存申请的时候,老样子^_^
- 重载<<为了接下来打印使用。
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;
- 构造map的方式之一,不多介绍
- 以Person类型的对象为索引(可以看作广义上的数组)
- 对应上面的构造函数,age为默认值0,此处意为在索引Person(“Jenny”, 0)处将内容置为”Newark”。
- 以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;
}