Point
1. operator< 오버로딩
전역으로는 두 객체를 받아 비교하지만 클래스 내부에서는 첫 번째 멤버는 this로 고정이므로 하나의 매개변수를 받는다.
2. 범위형 반복문에서 객체를 const로 선언한다면 getter도 const 선언이 되어야 한다.
3. 비교 연산이 < 일 경우 오름차순, > 일 경우 내림차순이다. STD 컨테이너는 기본적으로 오름차순이다.
Problem
In this exercise, you will implement a small inventory management system using C++ with Product objects as keys in a std::map. Each Product should have an id (integer) and a name (string). The std::map should automatically sort Product objects by their id using custom sorting logic by overloading the < operator.
Here's what you need to accomplish:
Define the Product class: Include id and name attributes, and overload < to enable sorting by id.
Create and populate the inventory: Define a std::map<Product, int> named inventory where the key is a Product and the value is the stock quantity. Add several Product objects with different id and name values and their respective stock quantities.
Implement product search: Write a findProductById function to search for a Product by id in the inventory. It should find the first product with an id not less than the specified search value and return its name and stock quantity. If no match is found, return a message indicating that the product was not found.
Display the result: Use findProductById to find and print a product by id without removing it from the map.
This task combines object-oriented programming with sorted data structures in C++, focusing on custom sorting and efficient map search. Follow the steps and structure your code accordingly.
#include "solution.hpp"
// TODO: Define a Product class with attributes id (integer) and name (string).
// Implement operator< for sorting by id.
class Product {
private:
int id;
std::string name;
public:
Product(int id, std::string name) {
this->id = id;
this->name = name;
}
bool operator<(const Product& other) const {
return this->id < other.id;
}
int getId() const {
return this->id;
}
std::string getName() const {
return this->name;
}
};
std::string findProductById(const std::map<Product, int>& inventory, int id) {
std::string result;
for (const auto& pair : inventory) {
if (pair.first.getId() >= id) {
result = pair.first.getName() + ", " + std::to_string(pair.second);
}
}
if (result.empty()) {
return "The product was not found";
} else {
return result;
}
}
int main() {
// TODO: Create an instance of std::map named inventory.
std::map<Product, int> inventory;
// TODO: Add some Product objects to inventory with different ids and names,
// associating each Product with an integer stock quantity.
inventory.insert(std::make_pair(Product(1, "Ice"), 5));
inventory.insert(std::make_pair(Product(2, "Cream"), 3));
inventory.insert(std::make_pair(Product(3, "Cookie"), 7));
inventory.insert(std::make_pair(Product(6, "Snack"), 4));
// TODO: Implement and use a function to find the first Product object with
// an id not less than a specified value, then print it without removing.
std::cout << findProductById(inventory, 5) << std::endl;
return 0;
}
'테크 > CodeSignal-C++' 카테고리의 다른 글
7. CodeSignal Course Certificate Advanced Built-In Data Structures and their Usage in C++ (0) | 2024.12.01 |
---|---|
6. Browsing system for Deque (0) | 2024.11.30 |
4. Multimap to Map (0) | 2024.11.28 |
3. Map & Multimap (0) | 2024.11.26 |
2. Queue and Deque (0) | 2024.11.23 |