#pragma once
#include"Book.h"
#include<vector>
class BookShelf {
private:
std::vector<Book> books;
public:
class Iterator;
friend class Iterator;
class Iterator {
BookShelf& bookshelf;
int index;
public:
Iterator(BookShelf& bs,int id=0) :bookshelf(bs),index(id){};
bool hasNext() {
if (index<bookshelf.books.size()) {
return true;
}
return false;
}
Book next() {
return bookshelf.books[index++];
}
};
void pushBook(Book book) {
books.push_back(book);
}
};
迭代器模式C++实现
最新推荐文章于 2023-09-24 10:36:59 发布