[**C++嵌套容器的查询**]

解决类里面包含容器的遍历,采用方法是函数的封装。

例:
查询书名的作者是谁;
作者类中包括书的容器;
在遍历每一个作者时候,调用遍历作者拥有书的容器。
此时把作者对象里容器遍历进行了封装。


#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Book
{
    string m_strBookNO;     //书号
    string m_strBookName;   //书名
    string m_strPublic;     //出版社
public:
    Book(string strNO, string strName, string strPublic) :m_strBookNO(strNO),
        m_strBookName(strName), m_strPublic(strPublic)
    {}
    string GetBookNO() { return m_strBookNO; }
    string GetBookName() { return m_strBookName; }
    void display_Book()
    {
        cout << "书号 : " << m_strBookNO << endl;
        cout << "书名 :  " << m_strBookName << endl;
        cout << "出版社 : " <<m_strPublic << endl;
    }

};
class Writer
{
    string m_strNO;     //作者编号
    string m_strName;   //作者姓名
    vector<Book> m_vecBook;//所拥有书的向量集合
    typedef vector<Book>::iterator iterator;
public:
    Writer(string strNO, string strName) :m_strNO(strNO), m_strName(strName) {}
    void AddBook(Book& book) {  //向该作者添加所著书籍
        m_vecBook.push_back(book);
    }
    string GetNO() { return m_strNO; }
    string GetName() { return m_strName; }
    vector<Book> GetVecBook() { return m_vecBook; }
    void display_Writer()
    {
        cout << "作者编号 :  " << m_strNO << endl;
        cout << "作者姓名 : " << m_strName << endl;
    }
    bool Find_writer(string strName)
    {
        for (iterator book = m_vecBook.begin();
        book != m_vecBook.end();++book)
        {
            Book& b = *book;
            if (b.GetBookName() == strName)
            {
                b.display_Book();
                return true;
                break;
            }
        }
        return false;
    }
};
class WriterCollect
{
    vector<Writer> m_vecWriter;
    typedef vector<Writer>::iterator iterator;

public:
    void AddWriter(Writer& writer) {//添加新的作者
        m_vecWriter.push_back(writer);
    }

    void Find(string strName) {
        for (iterator i = m_vecWriter.begin();i != m_vecWriter.end();++i)
        {
            Writer& s = *i;
            if (s.Find_writer(strName))
                s.display_Writer();
        }           
    }

};
void main()
{
    Writer w1("1001", "zhangsan");//产生作者对象w1
    Book b1("b001", "aaa", "public");//产生两本书对象
    Book b2("b002", "bbb", "public");
    w1.AddBook(b1);    //把该两本书加入作者对象w1
    w1.AddBook(b2);

    Writer w2("1002", "lisi");//产生作者对象w2
    Book b3("b003", "ccc", "public");//产生一本书对象
    w2.AddBook(b3);     //把此书加入作者对象w2

    WriterCollect collect;//产生作者集合类对象collect
    collect.AddWriter(w1);//向collect对象中加入作者对象w1
    collect.AddWriter(w2);//向collect对象中加入作者对象w2

    collect.Find("ccc");
    collect.Find("bbb");

    system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值