用c语言设计一个简单的图书馆管理系统,使用STL中的vector或list来实现对图书的增删改查操作,.具体要求: (1)使用vector或list作为数据结构来存储图书信息(vector顺序表、list双向
时间: 2024-09-27 12:07:53 浏览: 48
为了设计一个简单的图书馆管理系统,我们可以使用C++标准库中的`std::vector`或`std::list`容器来管理图书。这里我们以`std::vector`为例,因为它的访问速度较快,适合插入和删除操作频繁的场景。我们将创建一个图书类(Book),包含书名、作者和索引等属性,并提供相应的增删改查功能。
首先,我们需要定义图书类(Book):
```cpp
#include <string>
#include <vector>
class Book {
public:
std::string title;
std::string author;
int index;
// 构造函数
Book(const std::string& title, const std::string& author) : title(title), author(author), index(-1) {}
// 检查书籍是否已存在于集合中
bool isExist(std::vector<Book>& library) const {
return std::find(library.begin(), library.end(), *this) != library.end();
}
// 插入书籍到图书馆
void insert(std::vector<Book>& library) {
if (isExist(library)) {
throw std::runtime_error("Book already exists in the library.");
}
library.push_back(*this);
}
// 删除书籍
void remove(std::vector<Book>& library) {
auto it = find(library.begin(), library.end(), *this);
if (it != library.end()) {
library.erase(it);
} else {
throw std::runtime_error("Book not found in the library.");
}
}
private:
// 查找书籍
static Book* find(const std::vector<Book>& library, const Book& book) {
for (auto& item : library) {
if (item == book) {
return &item;
}
}
return nullptr;
}
};
```
接下来,可以创建一个主函数来实现基本的操作:
```cpp
int main() {
std::vector<Book> library;
try {
// 添加书籍
library.push_back(Book{"The Great Gatsby", "F. Scott Fitzgerald"});
library.push_back(Book{"To Kill a Mockingbird", "Harper Lee"});
// 查看图书馆内容
for (const auto& book : library) {
std::cout << "Title: " << book.title << ", Author: " << book.author << std::endl;
}
// 修改书籍信息
for (auto& book : library) {
if (book.title == "The Great Gatsby") {
book.author = "Modified by AI";
break;
}
}
// 删除一本书
library.remove(library[0]); // 注意索引从0开始
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}
```
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)