gpt4 book ai didi

c++ - 仅在多重映射中找到一个元素及其值 c++

转载 作者:行者123 更新时间:2023-11-30 03:40:31 25 4
gpt4 key购买 nike

首先很抱歉,如果我问的是愚蠢的问题,但我是 c++ 的初学者。

我正在编写一个代表图书馆的系统,我的图书馆类有一个成员函数应该允许我们删除一本书。现在,如果这本书是由用户借出的,则意味着我的 _usersLoaningMultimap 中有一个元素。 (multimap<UserId,LoanInfo>)。如何在不知道 key (UserId)的情况下找到我想要的 LoanInfo?

bool Library::removeBook(const BookId& bookId){
//how to find my book in my library without knowing who loaned it.

为了更清楚,我的类库是这样的:

class Library {
public:
Library();
void addUser(const UserId&, const string&);
Optional<string>& getUserInfo(const UserId& userId);
void addBook(const BookId& bookId, const string& description);
Optional<string>& getBookInfo(const BookId& bookId);
bool returnBook(const UserId& userId, const BookId& bookId);
void loanBook(const UserId& userId,LoanInfo& loan);
bool removeUser(const UserId& userId);
void getLoansSortedByDate(const UserId,std::vector<LoanInfo>& loanVector);

~Library() {}
private:
map<BookId, string> _bookMap;
map<UserId, string> _userMap;
multimap<UserId, LoanInfo> _usersLoaningMultimap;

};

最佳答案

你必须像这样遍历整个 map :

for(multimap<userId,LoanInfo>::iterator it = _usersLoaningMultimap.begin(); it != _usersLoaningMultimap.end(); it++){
//it->first retrieves key and it->second retrieves value
if(it->second == loan_info_you_are_searching){
//do whatever
}
}

关于c++ - 仅在多重映射中找到一个元素及其值 c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37950521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com