Skip to content

Commit f9c8101

Browse files
committed
added keys() function in set
1 parent 3b5a9a4 commit f9c8101

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ cmake -S . -B build
6262
Then open the generated ```functional_cpp.sln``` in the ```build``` folder.
6363

6464
## Functional vector usage (fcpp::vector)
65-
### extract unique (distinct) elements
65+
### extract unique (distinct) elements in a set
6666
```c++
6767
#include "vector.h" // instead of <vector>
6868

6969
const fcpp::vector<int> numbers({1, 4, 2, 5, 8, 3, 1, 7, 1});
7070

7171
// contains only 1, 2, 3, 4, 5, 7, 8
72-
const auto& unique_numbers = numbers.distinct();
72+
const fcpp::set unique_numbers = numbers.distinct();
7373
```
7474
7575
### zip, map, filter, sort
@@ -316,6 +316,9 @@ const fcpp::set<person, person_comparator> family({
316316
// all of our friends and family for the next party invitation
317317
// contains person(51, "George"), person(41, "Jackie"), person(42, "Crystal"), person(51, "Paul"), person(81, "Barbara")
318318
const auto friends_and_family = friends.union_with(family);
319+
320+
// all set keys in a vetor
321+
const fcpp::vector<person> = friends_and_family.keys();
319322
```
320323
321324
### zip, map, filter

include/set.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,15 @@ class set
437437
return *this;
438438
}
439439

440+
vector<TKey> keys() const {
441+
vector<TKey> vec;
442+
vec.reserve(size());
443+
for_each([&vec](const TKey& key) {
444+
vec.insert_back(key);
445+
});
446+
return std::move(vec);
447+
}
448+
440449
// Removes an element from the set, if it exists, potentially changing the set's contents (mutating)
441450
//
442451
// example:

tests/set_test.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,13 @@ TEST(SetTest, ZipWithStdVectorDifferentSizes)
436436
EXPECT_DEATH(ages.zip(persons), "");
437437
}
438438

439+
TEST(SetTest, Keys)
440+
{
441+
const set<int> numbers({ 25, 45, 30, 63 });
442+
const auto keys = numbers.keys();
443+
EXPECT_EQ(vector<int>({25, 30, 45, 63}), keys);
444+
}
445+
439446
TEST(SetTest, RemoveExistingElement)
440447
{
441448
set<int> numbers({1, 4, 2});

0 commit comments

Comments
 (0)