8000 Add numeric range in vector with sorting example · amalbansode/numeric-range@5f8bb30 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f8bb30

Browse files
committed
Add numeric range in vector with sorting example
1 parent 8719517 commit 5f8bb30

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

example/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
#list(APPEND example_sources
2-
# ${numeric_range_sources})
3-
add_executable(range_map ${numeric_range_sources} ${CMAKE_CURRENT_LIST_DIR}/range_map.cpp)
1+
add_executable(range_map ${numeric_range_sources} ${CMAKE_CURRENT_LIST_DIR}/range_map.cpp)
2+
3+
add_executable(range_vector ${numeric_range_sources} ${CMAKE_CURRENT_LIST_DIR}/range_vector.cpp)

example/range_vector.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "../src/numeric_range.hpp"
2+
3+
#include <algorithm>
4+
#include <iostream>
5+
#include <vector>
6+
7+
// Helper function to print a numeric range using math-esque notation of
8+
// bound inclusivity/exclusivity.
9+
template <typename T>
10+
void print_numeric_range (std::ostream& os, const NumericRange<T>& nr)
11+
{
12+
if (nr.lb_inclusive)
13+
os << "[";
14+
else
15+
os << "(";
16+
17+
os << nr.lb << ", " << nr.ub;
18+
19+
if (nr.ub_inclusive)
20+
os << "]";
21+
else
22+
os << ")";
23+
}
24+
25+
int main ()
26+
{
27+
// Remember to use the custom comparator
28+
std::vector<NumericRange<double> > range_vector;
29+
30+
// Populate the vector with some sample ranges and a mix of bound types
31+
range_vector.emplace_back(NumericRange<double>{0.0, true, 1.0, false});
32+
range_vector.emplace_back(NumericRange<double>{3.0, false, 3.1, false});
33+
range_vector.emplace_back(NumericRange<double>{-5.0, false, -1.0, true});
34+
range_vector.emplace_back(NumericRange<double>{1.1, true, 2.1, true});
35+
36+
// Iterate through the map and print elements (unsorted)
37+
std::cout << "Unsorted elements:" << std::endl;
38+
for (auto & i : range_vector)
39+
{
40+
print_numeric_range(std::cout, i);
41+
std::cout << std::endl;
42+
}
43+
44+
// Sort the vector using the comparator function in the library
45+
std::sort(range_vector.begin(), range_vector.end(), NumericRangeComparator<double>());
46+
47+
// Iterate through the map and print elements (sorted)
48+
std::cout << "Sorted elements:" << std::endl;
49+
for (auto & i : range_vector)
50+
{
51+
print_numeric_range(std::cout, i);
52+
std::cout << std::endl;
53+
}
54+
55+
return 0;
56+
}

0 commit comments

Comments
 (0)
0