Learn C++ · lesson 47 of 61 · about 10 minutes
Capstone: Core Logic
Add equipping, unequipping and searching to the armoury, with clear errors for bad requests.
What you'll learn
- Change objects through the map
- Throw for invalid operations
- Search and return results in a vector
Orders for the armoury
Now the hero gives the armoury orders until done: equip 1, unequip 2, or kind weapon to list every weapon. The rules live in Armoury. equip() finds the item with items_.at(id), which already throws std::out_of_range for an ID that isn't there, then throws std::logic_error if the item is already equipped; unequip() does the same the other way round. Both return the item, so main can say Equipped the sword, but only once the call has worked: if it throws, nothing should be printed yet. ofKind() returns the names of one kind in a vector. Main catches std::out_of_range first, as No item #9, then any other std::logic_error, printing its message.
Rules as code
borrow(id)throws std::out_of_range for an unknown ID and std::logic_error if the book is already outgive_back(id)throws if the book wasn't on loanby_author(name)returns the matching titles in a vector
books_.at(id) already throws std::out_of_range for a missing key, so we get that rule for free.
Borrowing and searching
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>
#include <vector>
struct Book {
std::string title;
std::string author;
bool on_loan = false;
};
class Library {
public:
int add(const std::string& title, const std::string& author) {
books_[next_id_] = Book{title, author};
return next_id_++;
}
void borrow(int id) {
Book& b = books_.at(id);
if (b.on_loan) throw std::logic_error(b.title + " is already on loan");
b.on_loan = true;
}
void give_back(int id) {
Book& b = books_.at(id);
if (!b.on_loan) throw std::logic_error(b.title + " wasn't borrowed");
b.on_loan = false;
}
std::vector<std::string> by_author(const std::string& author) const {
std::vector<std::string> found;
for (const auto& [id, b] : books_) if (b.author == author) found.push_back(b.title);
return found;
}
private:
std::map<int, Book> books_;
int next_id_ = 1;
};
int main() {
Library lib;
int dune = lib.add("Dune", "Herbert");
lib.add("Emma", "Austen");
lib.add("Persuasion", "Austen");
lib.borrow(dune);
for (int id : {dune, 99}) {
try {
lib.borrow(id);
} catch (const std::out_of_range&) {
std::cout << "No book " << id << "\n";
} catch (const std::logic_error& e) {
std::cout << e.what() << "\n";
}
}
for (const auto& t : lib.by_author("Austen")) std::cout << t << "\n";
}Output
Dune is already on loan
No book 99
Emma
PersuasionTip
std::out_of_range is itself a kind of std::logic_error, so catch it first. Otherwise the logic_error catch would grab it.
Giving back what was changed
Inside equip(), Item& item = items_.at(id); is a reference to the item in the map, so setEquipped(true) changes the stored item, not a copy. Returning it as const Item& lets main read the name straight away without being able to change anything.
Changing a value inside a map
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, int> power = {{1, 12}, {2, 18}};
int& sword = power.at(1);
sword += 3;
std::cout << "sword: " << power.at(1) << "\n";
}Output
sword: 15Now try it yourself
This lesson is free in Codiqo, with 10 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.