Skip to content

Commit 7e5c0b0

Browse files
committed
Added set algorithm examples.
1 parent 29d5e38 commit 7e5c0b0

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

STL_examples.cpp

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ void merge_sort(Iter first, Iter last) {
706706

707707
TEST(inplace_merge, ExampleOne) {
708708
std::vector<int> v{9, 3, -4, 4, 8, 9, 2, 2};
709-
// See Implementation above.
709+
// See implementation above.
710710
merge_sort(v.begin(), v.end());
711711

712712
const std::vector<int> v_sorted{-4, 2, 2, 3, 4, 8, 9, 9};
@@ -715,23 +715,81 @@ TEST(inplace_merge, ExampleOne) {
715715

716716
// Set operations (on sorted ranges).
717717
TEST(includes, ExampleOne) {
718+
// std::includes returns true if the first sorted range
719+
// is a non-contiguous subsequence of the second sorted range.
720+
const std::vector<char> v{'a', 'b', 'c', 'd', 'e', 'f'};
718721

722+
const std::vector<char> sub_v1{'a', 'b', 'c'};
723+
const bool v1_is_included = std::includes(v.cbegin(), v.cend(), sub_v1.cbegin(), sub_v1.cend());
724+
EXPECT_TRUE(v1_is_included);
725+
726+
const std::vector<char> sub_v2{'a', 'c', 'f'};
727+
const bool v2_is_included = std::includes(v.cbegin(), v.cend(), sub_v2.cbegin(), sub_v2.cend());
728+
EXPECT_TRUE(v2_is_included);
729+
730+
const std::vector<char> sub_v3{'a', 'c', 'x'};
731+
const bool v3_is_included = std::includes(v.cbegin(), v.cend(), sub_v3.cbegin(), sub_v3.cend());
732+
EXPECT_FALSE(v3_is_included);
719733
}
720734

721735
TEST(set_difference, ExampleOne) {
736+
// Takes the difference between the two sorted ranges.
737+
const std::vector<char> v1{'a', 'b', 'c', 'd', 'e', 'f'};
738+
const std::vector<char> v2{'b', 'c', 'd'};
739+
std::vector<char> difference;
740+
std::set_difference(v1.cbegin(), v1.cend(), v2.cbegin(), v2.cend(), std::back_inserter(difference));
722741

742+
const std::vector expected_difference{'a', 'e', 'f'};
743+
EXPECT_EQ(difference, expected_difference);
723744
}
724745

725746
TEST(set_intersection, ExampleOne) {
747+
// Takes the intersection between two sorted ranges.
748+
const std::vector<int> v1{1,2,3,4,5,6};
749+
const std::vector<int> v2{4,5,6,7,8,9};
750+
std::vector<int> intersection;
751+
752+
std::set_intersection(v1.cbegin(), v1.cend(), v2.cbegin(), v2.cend(), std::back_inserter(intersection));
726753

754+
const std::vector<int> expected_intersection{4,5,6};
755+
EXPECT_EQ(intersection, expected_intersection);
727756
}
728757

729758
TEST(set_symmetric_difference, ExampleOne) {
759+
// Takes the symmetric difference between two sorted ranges.
760+
const std::vector<int> v1{1,2,3,4,5,6};
761+
const std::vector<int> v2{4,5,6,7,8,9};
762+
std::vector<int> symmetric_difference;
730763

764+
std::set_symmetric_difference(v1.cbegin(), v1.cend(), v2.cbegin(), v2.cend(),
765+
std::back_inserter(symmetric_difference));
766+
767+
const std::vector<int> expected_symmetric_difference{1,2,3,7,8,9};
768+
EXPECT_EQ(symmetric_difference, expected_symmetric_difference);
731769
}
732770

733771
TEST(set_union, ExampleOne) {
772+
// Takes the union between two sorted ranges.
773+
const std::vector<int> v1{1,2,3,4,5,6};
774+
const std::vector<int> v2{4,5,6,7,8,9};
775+
std::vector<int> union_t;
776+
777+
std::set_union(v1.cbegin(), v1.cend(), v2.cbegin(), v2.cend(), std::back_inserter(union_t));
778+
779+
const std::vector<int> expected_union{1,2,3,4,5,6,7,8,9};
780+
EXPECT_EQ(union_t, expected_union);
781+
}
782+
783+
TEST(set_union, ExampleTwoWithDuplicates) {
784+
// Takes the union between two sorted ranges.
785+
const std::vector<int> v1{1,1,2,3,4,5,6};
786+
const std::vector<int> v2{1,1,1,4,5,6,7,8,9};
787+
std::vector<int> union_t;
788+
789+
std::set_union(v1.cbegin(), v1.cend(), v2.cbegin(), v2.cend(), std::back_inserter(union_t));
734790

791+
const std::vector<int> expected_union{1,1,1,2,3,4,5,6,7,8,9};
792+
EXPECT_EQ(union_t, expected_union);
735793
}
736794

737795
// Heap operations.

0 commit comments

Comments
 (0)