Initial Course
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
add_course_exercise(NAME sequential_containers MODULE 07_stl_containers STANDARD 17
|
||||
SOURCES src/sequential_containers.cpp test/sequential_containers_test.cpp)
|
||||
add_course_exercise(NAME associative_containers MODULE 07_stl_containers STANDARD 17
|
||||
SOURCES src/associative_containers.cpp test/associative_containers_test.cpp)
|
||||
add_course_exercise(NAME unordered_containers MODULE 07_stl_containers STANDARD 17
|
||||
SOURCES src/unordered_containers.cpp test/unordered_containers_test.cpp)
|
||||
add_course_exercise(NAME container_adapters MODULE 07_stl_containers STANDARD 17
|
||||
SOURCES src/container_adapters.cpp test/container_adapters_test.cpp)
|
||||
@@ -0,0 +1,17 @@
|
||||
# Module 07: STL Containers
|
||||
|
||||
## Learning Goals
|
||||
|
||||
- Choose the right sequential container for the job
|
||||
- Use associative containers (`map`, `set`) effectively
|
||||
- Leverage unordered containers for average O(1) lookup
|
||||
- Apply container adapters (`stack`, `queue`)
|
||||
|
||||
## Exercises
|
||||
|
||||
| Exercise | Topic |
|
||||
|----------|-------|
|
||||
| `sequential_containers` | vector, list, deque |
|
||||
| `associative_containers` | map, set, multimap |
|
||||
| `unordered_containers` | unordered_map, unordered_set |
|
||||
| `container_adapters` | stack, queue, priority_queue |
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace stl_containers {
|
||||
|
||||
[[nodiscard]] auto word_frequencies(const std::vector<std::string>& words)
|
||||
-> std::map<std::string, int>;
|
||||
[[nodiscard]] auto unique_sorted(const std::vector<int>& data) -> std::set<int>;
|
||||
[[nodiscard]] auto invert_map(const std::map<int, std::string>& input)
|
||||
-> std::map<std::string, int>;
|
||||
|
||||
} // namespace stl_containers
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <queue>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace stl_containers {
|
||||
|
||||
[[nodiscard]] auto is_balanced_parentheses(const std::string& text) -> bool;
|
||||
[[nodiscard]] auto simulate_queue(const std::vector<int>& arrivals) -> std::vector<int>;
|
||||
[[nodiscard]] auto top_k_largest(const std::vector<int>& data, int k) -> std::vector<int>;
|
||||
|
||||
} // namespace stl_containers
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <deque>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
|
||||
namespace stl_containers {
|
||||
|
||||
[[nodiscard]] auto merge_sorted_vectors(const std::vector<int>& a, const std::vector<int>& b)
|
||||
-> std::vector<int>;
|
||||
[[nodiscard]] auto list_to_vector(const std::list<int>& data) -> std::vector<int>;
|
||||
[[nodiscard]] auto rotate_deque(std::deque<int> data, int steps) -> std::deque<int>;
|
||||
|
||||
} // namespace stl_containers
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
namespace stl_containers {
|
||||
|
||||
[[nodiscard]] auto first_unique(const std::vector<std::string>& words) -> std::string;
|
||||
[[nodiscard]] auto group_anagrams(const std::vector<std::string>& words)
|
||||
-> std::unordered_map<std::string, std::vector<std::string>>;
|
||||
[[nodiscard]] auto has_duplicate(const std::vector<int>& data) -> bool;
|
||||
|
||||
} // namespace stl_containers
|
||||
@@ -0,0 +1,21 @@
|
||||
#include "associative_containers.hpp"
|
||||
|
||||
namespace stl_containers {
|
||||
|
||||
auto word_frequencies(const std::vector<std::string>& words)
|
||||
-> std::map<std::string, int> {
|
||||
(void)words;
|
||||
return {};
|
||||
}
|
||||
|
||||
auto unique_sorted(const std::vector<int>& data) -> std::set<int> {
|
||||
(void)data;
|
||||
return {};
|
||||
}
|
||||
|
||||
auto invert_map(const std::map<int, std::string>& input) -> std::map<std::string, int> {
|
||||
(void)input;
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace stl_containers
|
||||
@@ -0,0 +1,20 @@
|
||||
#include "container_adapters.hpp"
|
||||
|
||||
namespace stl_containers {
|
||||
|
||||
auto is_balanced_parentheses(const std::string& text) -> bool {
|
||||
(void)text;
|
||||
return false;
|
||||
}
|
||||
|
||||
auto simulate_queue(const std::vector<int>& arrivals) -> std::vector<int> {
|
||||
(void)arrivals;
|
||||
return {};
|
||||
}
|
||||
|
||||
auto top_k_largest(const std::vector<int>& data, int k) -> std::vector<int> {
|
||||
(void)data; (void)k;
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace stl_containers
|
||||
@@ -0,0 +1,21 @@
|
||||
#include "sequential_containers.hpp"
|
||||
|
||||
namespace stl_containers {
|
||||
|
||||
auto merge_sorted_vectors(const std::vector<int>& a, const std::vector<int>& b)
|
||||
-> std::vector<int> {
|
||||
(void)a; (void)b;
|
||||
return {};
|
||||
}
|
||||
|
||||
auto list_to_vector(const std::list<int>& data) -> std::vector<int> {
|
||||
(void)data;
|
||||
return {};
|
||||
}
|
||||
|
||||
auto rotate_deque(std::deque<int> data, int steps) -> std::deque<int> {
|
||||
(void)data; (void)steps;
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace stl_containers
|
||||
@@ -0,0 +1,21 @@
|
||||
#include "unordered_containers.hpp"
|
||||
|
||||
namespace stl_containers {
|
||||
|
||||
auto first_unique(const std::vector<std::string>& words) -> std::string {
|
||||
(void)words;
|
||||
return {};
|
||||
}
|
||||
|
||||
auto group_anagrams(const std::vector<std::string>& words)
|
||||
-> std::unordered_map<std::string, std::vector<std::string>> {
|
||||
(void)words;
|
||||
return {};
|
||||
}
|
||||
|
||||
auto has_duplicate(const std::vector<int>& data) -> bool {
|
||||
(void)data;
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace stl_containers
|
||||
@@ -0,0 +1,18 @@
|
||||
#include "associative_containers.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(AssociativeContainers, WordFrequencies) {
|
||||
const auto freq = stl_containers::word_frequencies({"a", "b", "a"});
|
||||
EXPECT_EQ(freq.at("a"), 2);
|
||||
EXPECT_EQ(freq.at("b"), 1);
|
||||
}
|
||||
|
||||
TEST(AssociativeContainers, UniqueSorted) {
|
||||
EXPECT_EQ(stl_containers::unique_sorted({3, 1, 2, 2}), (std::set<int>{1, 2, 3}));
|
||||
}
|
||||
|
||||
TEST(AssociativeContainers, InvertMap) {
|
||||
const std::map<int, std::string> input{{1, "one"}, {2, "two"}};
|
||||
const auto inverted = stl_containers::invert_map(input);
|
||||
EXPECT_EQ(inverted.at("one"), 1);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "container_adapters.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(ContainerAdapters, BalancedParentheses) {
|
||||
EXPECT_TRUE(stl_containers::is_balanced_parentheses("()[]{}"));
|
||||
EXPECT_FALSE(stl_containers::is_balanced_parentheses("([)]"));
|
||||
}
|
||||
|
||||
TEST(ContainerAdapters, SimulateQueue) {
|
||||
EXPECT_EQ(stl_containers::simulate_queue({1, 2, 3}), (std::vector<int>{1, 2, 3}));
|
||||
}
|
||||
|
||||
TEST(ContainerAdapters, TopKLargest) {
|
||||
EXPECT_EQ(stl_containers::top_k_largest({3, 1, 4, 1, 5}, 3), (std::vector<int>{5, 4, 3}));
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "sequential_containers.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(SequentialContainers, MergeSorted) {
|
||||
EXPECT_EQ(stl_containers::merge_sorted_vectors({1, 3, 5}, {2, 4, 6}),
|
||||
(std::vector<int>{1, 2, 3, 4, 5, 6}));
|
||||
}
|
||||
|
||||
TEST(SequentialContainers, ListToVector) {
|
||||
EXPECT_EQ(stl_containers::list_to_vector({1, 2, 3}), (std::vector<int>{1, 2, 3}));
|
||||
}
|
||||
|
||||
TEST(SequentialContainers, RotateDeque) {
|
||||
EXPECT_EQ(stl_containers::rotate_deque({1, 2, 3, 4}, 1), (std::deque<int>{2, 3, 4, 1}));
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#include "unordered_containers.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(UnorderedContainers, FirstUnique) {
|
||||
EXPECT_EQ(stl_containers::first_unique({"a", "b", "a", "c"}), "b");
|
||||
}
|
||||
|
||||
TEST(UnorderedContainers, HasDuplicate) {
|
||||
EXPECT_TRUE(stl_containers::has_duplicate({1, 2, 2}));
|
||||
EXPECT_FALSE(stl_containers::has_duplicate({1, 2, 3}));
|
||||
}
|
||||
Reference in New Issue
Block a user