Design and implement a 'Name_pairs' class holding (name,age) pairs where name is a 'string' and age

本文介绍了一个 C++ 实现的 Name_pairs 类,该类能够存储姓名与年龄的配对,并提供读取、打印和排序等功能。此外还实现了比较运算符的重载,并使用 std::ostream 进行输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于Programming – Principles and Practice Using C++ (Stroustrup)其中一道练习题的解答:
题目如下:
【1】Design and implement a ‘Name_pairs’ class holding (name,age) pairs where name is a ‘string’ and age is a ‘double’. Represent that as a ‘vector’ (called ‘name’) and a ‘vector’ (called ‘age’) member. Provide an input operation ‘red_names()’ that reads a series of names. Provide a ‘read_ages()’ operation that prompts the user for an age for each name. Provide a ‘print()’ operation that prints out the (‘name[i], age[i]’) pairs (one per line) in the order determined by the ‘name’ vector. Provide a ‘sort()’ operation that sorts the ‘name’ vector in alphabetical order and reorganizes the ‘age’ vector to match. Implement all operations as member functions.
【2】Redefine the operator===and != for Name_pairs.Try to replace Name_pair::print() with operator<<.
参考链接:
【1】http://www.cplusplus.com/forum/general/119640/
【2】http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/
【3】http://www.cplusplus.com/doc/tutorial/classes/
代码已运行调试通过,欢迎大家更新优化。

#include<iostream> 
#include<string>
#include<vector>



class Name_pairs 
{ 
public: 
    void Read_name(); 
    void Read_age();
    void print()const;
    void sort(); 
    std::string Name(int i)const;
    double Age(int i)const; 
    int length()const;
    //重载 == 操作符
    bool operator==(const Name_pairs & NA); 
    bool operator!=(const Name_pairs &NA);
    friend std::ostream & operator<<(std::ostream &os, const Name_pairs &NA);
private: 
    std::vector<std::string>names;
    std::vector<double>ages;
};

void Name_pairs::Read_name() 
{ 
    std::string name; 
    std::cout << "please input array name,exit with N:\n";
    while (std::cin >> name && name != "N")
        names.push_back(name); 
    if (!std::cin)
    { 
        std::cin.clear();
        std::cin.sync();
    } 
}
void Name_pairs::Read_age() 
{
    double age; 
    for (int i = 0; i<names.size(); i++) 
    {
        std::cout << "输入" << names[i] << "对应的年龄:\n";
        std::cin >> age;
        ages.push_back(age);
    }
}

void Name_pairs::print()const 
{ 
    for (int i = 0; i<names.size(); i++) 
        std::cout << names[i] << '\t' << ages[i] << '\n';
} 
void Name_pairs::sort()
{ 
    for (int i = 0; i<names.size() - 1; i++) 
        for (int j = i + 1; j<names.size(); j++) 
            if (names[i]>names[j]) 
            { 
                std::string tempname = names[i];
                names[i] = names[j];
                names[j] = tempname; 
                double tempage = ages[i]; 
                ages[i] = ages[j]; 
                ages[j] = tempage; 
            } 
}
std::string Name_pairs::Name(int i)const
{
    return names[i];
}


double Name_pairs::Age(int i)const 
{ 
    return ages[i];
}

int Name_pairs::length()const {

    return names.size();
}

bool Name_pairs::operator==(const Name_pairs& NA)
{ 
    if(this->length ()!=NA.length ()) 
        return false;
    for(int i=0;i<this->length ();i++) 
        if(this->Name(i)!=NA.Name(i) || this->Age(i) != NA.Age(i))
            return false; 
    return true; 
}
bool Name_pairs::operator!=(const Name_pairs& NA)
{
    return !(*this == NA);
}
std::ostream & operator<<(std::ostream &os, const Name_pairs &NA)
{
    os << "遍历了整个vector" << std::endl;
    for (int i = 0; i < NA.length(); i++) {
        os << NA.names[i] << '\t' << NA.ages[i] << '\n';
    }
    return os;
}
void main() 
{
    Name_pairs NA1; 
    Name_pairs NA2;
    std::cout << "--------------------question 1---------------------\n";
    NA1.Read_name(); 
    NA1.Read_age(); 
    NA1.print(); 
    NA1.sort(); 
    std::cout << "---------------------------------------------------\n";
    NA1.print();
    std::cout << "--------------------question 2---------------------\n";
    NA2.Read_name();
    NA2.Read_age();
    NA2.print();
    if (NA1.operator==(NA2))
    {
        std::cout << "NA1=NA2" << std::endl;
    }
    else
    {
        std::cout << "NA1!=NA2" << std::endl;
    };
    std::cout << NA1 << std::endl;
    system("pause");

}

运行结果为:
——————–question 1———————
please input array name,exit with N:
atest1
ctest2
dtest3
btest4
N
输入atest1对应的年龄:
12
输入ctest2对应的年龄:
13
输入dtest3对应的年龄:
43
输入btest4对应的年龄:
44
atest1 12
ctest2 13
dtest3 43

btest4 44

atest1 12
btest4 44
ctest2 13
dtest3 43
——————–question 2———————
please input array name,exit with N:
test3
test4
N
输入test3对应的年龄:
12
输入test4对应的年龄:
13
test3 12
test4 13
NA1!=NA2
遍历了整个vector
atest1 12
btest4 44
ctest2 13
dtest3 43

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值