Initial Course
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
add_course_exercise(NAME sort_and_find MODULE 08_stl_algorithms STANDARD 17
|
||||
SOURCES src/sort_and_find.cpp test/sort_and_find_test.cpp)
|
||||
add_course_exercise(NAME transform_reduce MODULE 08_stl_algorithms STANDARD 17
|
||||
SOURCES src/transform_reduce.cpp test/transform_reduce_test.cpp)
|
||||
add_course_exercise(NAME iterators MODULE 08_stl_algorithms STANDARD 17
|
||||
SOURCES src/iterators.cpp test/iterators_test.cpp)
|
||||
add_course_exercise(NAME algorithm_lambdas MODULE 08_stl_algorithms STANDARD 17
|
||||
SOURCES src/algorithm_lambdas.cpp test/algorithm_lambdas_test.cpp)
|
||||
@@ -0,0 +1,17 @@
|
||||
# Module 08: STL Algorithms
|
||||
|
||||
## Learning Goals
|
||||
|
||||
- Use `<algorithm>` for sorting, searching, and transforming
|
||||
- Understand iterator categories and valid operations
|
||||
- Combine algorithms with function objects and lambdas
|
||||
- Write expressive data-processing pipelines
|
||||
|
||||
## Exercises
|
||||
|
||||
| Exercise | Topic |
|
||||
|----------|-------|
|
||||
| `sort_and_find` | sort, binary_search, find |
|
||||
| `transform_reduce` | transform, accumulate, reduce |
|
||||
| `iterators` | Iterator patterns and adapters |
|
||||
| `algorithm_lambdas` | Lambdas with STL algorithms |
|
||||
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace stl_algorithms {
|
||||
|
||||
[[nodiscard]] auto filter_length(const std::vector<std::string>& words, std::size_t min_len)
|
||||
-> std::vector<std::string>;
|
||||
[[nodiscard]] auto map_to_lengths(const std::vector<std::string>& words) -> std::vector<int>;
|
||||
[[nodiscard]] auto first_match(const std::vector<int>& data, int threshold) -> int;
|
||||
|
||||
} // namespace stl_algorithms
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace stl_algorithms {
|
||||
|
||||
[[nodiscard]] auto reverse_copy(const std::vector<int>& data) -> std::vector<int>;
|
||||
[[nodiscard]] auto countGreaterThan(const std::vector<int>& data, int threshold) -> int;
|
||||
[[nodiscard]] auto everyEven(const std::vector<int>& data) -> bool;
|
||||
|
||||
} // namespace stl_algorithms
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace stl_algorithms {
|
||||
|
||||
[[nodiscard]] auto sorted_copy(std::vector<int> data) -> std::vector<int>;
|
||||
[[nodiscard]] auto contains(const std::vector<int>& data, int value) -> bool;
|
||||
[[nodiscard]] auto lower_bound_index(const std::vector<int>& sorted, int value) -> int;
|
||||
|
||||
} // namespace stl_algorithms
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace stl_algorithms {
|
||||
|
||||
[[nodiscard]] auto square_all(const std::vector<int>& data) -> std::vector<int>;
|
||||
[[nodiscard]] auto sum_all(const std::vector<int>& data) -> int;
|
||||
[[nodiscard]] auto product_positive(const std::vector<int>& data) -> long long;
|
||||
|
||||
} // namespace stl_algorithms
|
||||
@@ -0,0 +1,21 @@
|
||||
#include "algorithm_lambdas.hpp"
|
||||
|
||||
namespace stl_algorithms {
|
||||
|
||||
auto filter_length(const std::vector<std::string>& words, std::size_t min_len)
|
||||
-> std::vector<std::string> {
|
||||
(void)words; (void)min_len;
|
||||
return {};
|
||||
}
|
||||
|
||||
auto map_to_lengths(const std::vector<std::string>& words) -> std::vector<int> {
|
||||
(void)words;
|
||||
return {};
|
||||
}
|
||||
|
||||
auto first_match(const std::vector<int>& data, int threshold) -> int {
|
||||
(void)data; (void)threshold;
|
||||
return -1;
|
||||
}
|
||||
|
||||
} // namespace stl_algorithms
|
||||
@@ -0,0 +1,9 @@
|
||||
#include "iterators.hpp"
|
||||
|
||||
namespace stl_algorithms {
|
||||
|
||||
auto reverse_copy(const std::vector<int>& data) -> std::vector<int> { (void)data; return {}; }
|
||||
auto countGreaterThan(const std::vector<int>& data, int threshold) -> int { (void)data; (void)threshold; return 0; }
|
||||
auto everyEven(const std::vector<int>& data) -> bool { (void)data; return false; }
|
||||
|
||||
} // namespace stl_algorithms
|
||||
@@ -0,0 +1,9 @@
|
||||
#include "sort_and_find.hpp"
|
||||
|
||||
namespace stl_algorithms {
|
||||
|
||||
auto sorted_copy(std::vector<int> data) -> std::vector<int> { (void)data; return {}; }
|
||||
auto contains(const std::vector<int>& data, int value) -> bool { (void)data; (void)value; return false; }
|
||||
auto lower_bound_index(const std::vector<int>& sorted, int value) -> int { (void)sorted; (void)value; return -1; }
|
||||
|
||||
} // namespace stl_algorithms
|
||||
@@ -0,0 +1,9 @@
|
||||
#include "transform_reduce.hpp"
|
||||
|
||||
namespace stl_algorithms {
|
||||
|
||||
auto square_all(const std::vector<int>& data) -> std::vector<int> { (void)data; return {}; }
|
||||
auto sum_all(const std::vector<int>& data) -> int { (void)data; return 0; }
|
||||
auto product_positive(const std::vector<int>& data) -> long long { (void)data; return 0; }
|
||||
|
||||
} // namespace stl_algorithms
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "algorithm_lambdas.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(AlgorithmLambdas, FilterLength) {
|
||||
EXPECT_EQ(stl_algorithms::filter_length({"a", "ab", "abc"}, 2),
|
||||
(std::vector<std::string>{"ab", "abc"}));
|
||||
}
|
||||
|
||||
TEST(AlgorithmLambdas, MapToLengths) {
|
||||
EXPECT_EQ(stl_algorithms::map_to_lengths({"hi", "there"}), (std::vector<int>{2, 5}));
|
||||
}
|
||||
|
||||
TEST(AlgorithmLambdas, FirstMatch) {
|
||||
EXPECT_EQ(stl_algorithms::first_match({1, 5, 9}, 4), 5);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "iterators.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(Iterators, ReverseCopy) {
|
||||
EXPECT_EQ(stl_algorithms::reverse_copy({1, 2, 3}), (std::vector<int>{3, 2, 1}));
|
||||
}
|
||||
|
||||
TEST(Iterators, CountGreaterThan) {
|
||||
EXPECT_EQ(stl_algorithms::countGreaterThan({1, 5, 9}, 4), 2);
|
||||
}
|
||||
|
||||
TEST(Iterators, EveryEven) {
|
||||
EXPECT_TRUE(stl_algorithms::everyEven({2, 4, 6}));
|
||||
EXPECT_FALSE(stl_algorithms::everyEven({2, 3}));
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#include "sort_and_find.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(SortAndFind, SortedCopy) {
|
||||
EXPECT_EQ(stl_algorithms::sorted_copy({3, 1, 2}), (std::vector<int>{1, 2, 3}));
|
||||
}
|
||||
|
||||
TEST(SortAndFind, Contains) {
|
||||
EXPECT_TRUE(stl_algorithms::contains({1, 2, 3}, 2));
|
||||
}
|
||||
|
||||
TEST(SortAndFind, LowerBoundIndex) {
|
||||
EXPECT_EQ(stl_algorithms::lower_bound_index({1, 3, 5, 7}, 5), 2);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#include "transform_reduce.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(TransformReduce, SquareAll) {
|
||||
EXPECT_EQ(stl_algorithms::square_all({1, 2, 3}), (std::vector<int>{1, 4, 9}));
|
||||
}
|
||||
|
||||
TEST(TransformReduce, SumAll) {
|
||||
EXPECT_EQ(stl_algorithms::sum_all({1, 2, 3}), 6);
|
||||
}
|
||||
|
||||
TEST(TransformReduce, ProductPositive) {
|
||||
EXPECT_EQ(stl_algorithms::product_positive({-1, 2, 3, -4}), 6);
|
||||
}
|
||||
Reference in New Issue
Block a user