Principle: Red-Black Tree
Header: #include <map>
Methods: find(), count(), empty(), equal_rage()
Associated: std::next(), std::prev(), std::distance()
Feature: Map's key must be unique, multimap's key can be duplicated.
Code example:
#include "solution.hpp"
#include <iostream>
#include <map>
bool checkMultipleOccurrences(const std::string& fruit) {
// Create a multimap with fruits as keys and their counts as values
std::multimap<std::string, int> fruitMap = {
{"apple", 5},
{"banana", 2},
{"apple", 3},
{"orange", 4},
{"apple", 4}
};
// TODO: Determine if the given fruit has multiple values and return true or false
/*
if (fruitMap.count(fruit) > 1) {
return true;
} else {
return false;
}
*/
auto range = fruitMap.equal_range(fruit);
if (std::distance(range.first, range.second) > 1) {
return true;
} else {
return false;
}
}
int main() {
bool result = checkMultipleOccurrences("apple");
if (result) {
std::cout << "Multiple occurrences found." << std::endl;
} else {
std::cout << "No multiple occurrences." << std::endl;
}
return 0;
}
#include "solution.hpp"
std::map<std::string, int> count_event_frequency(const std::multimap<std::string, std::string>& events) {
// TODO: Implement logic to return the frequency count of each date in the multimap.
std::map<std::string, int> map;
// multimap에서 꺼내서 map의 키를 검사 후 넣어
for (const auto& event : events) {
int count = map.count(event.first);
if (map.count(event.first) != 0) {
map.erase(event.first);
}
map.insert({event.first, count+1});
}
return map;
}
int main() {
// Create a multimap containing dates and their respective events
std::multimap<std::string, std::string> events = {
{"2023-06-11", "Seminar"},
{"2023-06-11", "Workshop"},
{"2023-06-21", "Concert"},
{"2023-07-12", "Conference"},
{"2023-07-12", "Meeting"}
};
// Get the frequency count of events on each date
std::map<std::string, int> frequency_count = count_event_frequency(events);
// Display the frequency count of events
for (const auto& date : frequency_count) {
std::cout << date.first << ": " << date.second << std::endl;
}
return 0;
}
'테크 > CodeSignal-C++' 카테고리의 다른 글
6. Browsing system for Deque (0) | 2024.11.30 |
---|---|
5. Map using custom object key (0) | 2024.11.29 |
4. Multimap to Map (0) | 2024.11.28 |
2. Queue and Deque (0) | 2024.11.23 |
1. Stack (0) | 2024.11.23 |