Skip to content

Commit 24a2234

Browse files
bernhardmgruberhageboeck
authored andcommitted
Improve templates exercise
* Reword/extend README.md * Repeat some instructions inside the code * Use std::less instead of custom less * Already provide the implementation of RevOrderString, so students can focus on the template related stuff * Move to C++20 Co-authored-by: Stephan Hageboeck <[email protected]>
1 parent 323cf59 commit 24a2234

File tree

6 files changed

+48
-24
lines changed

6 files changed

+48
-24
lines changed

code/templates/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
1010
include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" )
1111
include( "${CMAKE_CURRENT_SOURCE_DIR}/../SolutionTarget.cmake" )
1212

13+
set(CMAKE_CXX_STANDARD 20)
14+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
15+
1316
# Create the user's executable.
1417
add_executable( playwithsort "Complex.hpp" "OrderedVector.hpp" "playwithsort.cpp" )
1518

code/templates/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ clean:
55
rm -f *o *so playwithsort *~ playwithsort.sol
66

77
playwithsort : playwithsort.cpp OrderedVector.hpp Complex.hpp
8-
$(CXX) -std=c++17 -g -O0 -Wall -Wextra -o $@ $<
8+
$(CXX) -std=c++20 -g -O0 -Wall -Wextra -o $@ $<
99

1010
playwithsort.sol : solution/playwithsort.sol.cpp solution/OrderedVector.sol.hpp Complex.hpp
11-
$(CXX) -std=c++17 -g -O0 -Wall -Wextra -I. -o $@ $<
11+
$(CXX) -std=c++20 -g -O0 -Wall -Wextra -I. -o $@ $<

code/templates/README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
## Instructions
33

44
Beginners
5-
* look at the `OrderedVector` code
6-
* compile and run `playwithsort.cpp`. See the ordering
7-
* modify `playwithsort.cpp` and reuse `OrderedVector` with `Complex`
5+
* Look at the `OrderedVector` code
6+
* Compile and run `playwithsort.cpp`. See the ordering
7+
* Modify `playwithsort.cpp` and reuse `OrderedVector` with `Complex`
88

99
Intermediary
10-
* improve `OrderedVector` to template the ordering
11-
* test reverse ordering of strings (from the last letter)
12-
* test order based on [Manhattan distance](https://en.wikipedia.org/wiki/Taxicab_geometry) with complex type
10+
* Extend `OrderedVector` to allow to customize the ordering via an additional template parameter.
11+
By default, `std::less` should be used.
12+
* Test ordering by the reversed strings (from the last letter, don't change the strings!)
13+
* Test order based on [Manhattan distance](https://en.wikipedia.org/wiki/Taxicab_geometry) with complex type
1314

1415
Bonus
15-
* check the implementation of `Complex`
16-
* try ordering complex of complex
16+
* Check the implementation of `Complex`
17+
* Try ordering complex of complex

code/templates/playwithsort.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
#include <string>
44
#include <iostream>
55
#include <algorithm>
6+
#include <ranges>
67

7-
/*
8-
struct OrderFunctor {
9-
bool operator() (const TYPE &s, const TYPE &t) const {
8+
struct ReverseStringLess {
9+
bool operator() (const std::string &s, const std::string &t) const {
10+
// TODO: compare reversed strings
11+
// hint: you can use:
12+
// - std::views::reverse
13+
// - std::ranges::lexicographical_compare
14+
// or, if your compiler does not support those yet,
15+
// take copies of the strings and reverse them using std::reverse
16+
return s < t;
1017
}
1118
};
12-
*/
1319

1420
int main() {
1521
std::cout << "Integer\n";
@@ -31,4 +37,15 @@ int main() {
3137
std::cout << vs[i] << " ";
3238
std::cout << "\n\n";
3339

40+
// TODO: Demonstrate OrderedVector with Complex as done above
41+
42+
43+
// TODO: Extend OrderedVector to allow to customize the ordering via an additional template paramter.
44+
// Then, demonstrate the new functionality by ordering an OrderedVector<std::string>,
45+
// where the strings are compared starting at their last letters.
46+
47+
48+
// TODO: Order an OrderedVector of Complex based on the Manhattan distance
49+
50+
3451
}

code/templates/solution/OrderedVector.sol.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
#include <memory>
22
#include <stdexcept>
3+
#include <functional>
34

4-
template <typename T>
5-
struct less {
6-
bool operator() (const T& x, const T& y) const {return x<y;}
7-
};
8-
9-
template<typename ElementType, typename Compare=less<ElementType> >
5+
template<typename ElementType, typename Compare=std::less<>>
106
class OrderedVector {
117
public:
128
OrderedVector(unsigned int maxLen);

code/templates/solution/playwithsort.sol.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@
44
#include <iostream>
55
#include <algorithm>
66
#include <cmath>
7+
#include <ranges>
78

8-
struct RevOrderString {
9+
struct ReverseStringLess {
910
bool operator() (const std::string &s, const std::string &t) const {
11+
// __cpp_lib_ranges is a feature test macro.
12+
// We can use those to check what features a C++ compiler supports.
13+
#ifdef __cpp_lib_ranges
14+
return std::ranges::lexicographical_compare(std::views::reverse(s), std::views::reverse(t));
15+
#else
1016
std::string rs = s;
1117
std::string rt = t;
1218
std::reverse(rs.begin(), rs.end());
1319
std::reverse(rt.begin(), rt.end());
1420
return rs < rt;
21+
#endif
1522
}
1623
};
1724

18-
struct ManhattanOrder {
25+
struct ManhattanLess {
1926
bool operator() (const Complex &a, const Complex &b) const {
2027
return std::abs(a.real()) + std::abs(a.imaginary()) < std::abs(b.real()) + std::abs(b.imaginary());
2128
}
@@ -53,7 +60,7 @@ int main() {
5360
std::cout << "\n\n";
5461

5562
std::cout << "String\n";
56-
OrderedVector<std::string, RevOrderString> vsr(5);
63+
OrderedVector<std::string, ReverseStringLess> vsr(5);
5764
vsr.add(std::string("one"));
5865
vsr.add(std::string("two"));
5966
vsr.add(std::string("three"));
@@ -64,7 +71,7 @@ int main() {
6471
std::cout << "\n\n";
6572

6673
std::cout << "Complex with manhatan order\n";
67-
OrderedVector<Complex, ManhattanOrder> vcm(5);
74+
OrderedVector<Complex, ManhattanLess> vcm(5);
6875
vcm.add(Complex(1.5,0.0));
6976
vcm.add(Complex(1.0,1.0));
7077
vcm.add(Complex(-1.0,0.0));

0 commit comments

Comments
 (0)