From 12b4d80723e4cf98c747aa0bb5219a40c8328186 Mon Sep 17 00:00:00 2001 From: jkaliak Date: Sun, 24 Sep 2023 20:44:59 +0200 Subject: [PATCH] contains --- include/map.h | 26 +++++++++++++------------- tests/map_test.cc | 14 +++++++------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/include/map.h b/include/map.h index 2c79f16..b1fdfad 100644 --- a/include/map.h +++ b/include/map.h @@ -550,19 +550,19 @@ class map { return m_map.empty(); } -// -// -// // Returns true if the key is present in the set, otherwise false -// // -// // example: -// // const fcpp::set numbers({1, 4, 2}); -// // numbers.contains(1); // true -// // numbers.contains(15); // false -// [[nodiscard]] bool contains(const TKey& key) const -// { -// return m_map.count(key) != 0; -// } -// + + + // Returns true if the key is present in the map, otherwise false + // + // example: + // const fcpp::map persons({{"jake", 32}, {"mary", 26}, {"david", 40}}); + // persons.contains("jake"); // true + // persons.contains("bob"); // false + [[nodiscard]] bool contains(const TKey& key) const + { + return m_map.find(key) != end(); + } + // Returns the size of the map (how many elements it contains, it may be different from its capacity) [[nodiscard]] size_t size() const { diff --git a/tests/map_test.cc b/tests/map_test.cc index 080809f..fb3ece7 100644 --- a/tests/map_test.cc +++ b/tests/map_test.cc @@ -478,13 +478,13 @@ TEST(MapTest, IsEmpty) EXPECT_TRUE(empty_map.is_empty()); } -//TEST(MapTest, Contains) -//{ -// const set numbers({1, 4, 2}); -// EXPECT_TRUE(numbers.contains(1)); -// EXPECT_FALSE(numbers.contains(15)); -//} -// +TEST(MapTest, Contains) +{ + const map persons({{"jake", 32}, {"mary", 26}, {"david", 40}}); + EXPECT_TRUE(persons.contains("jake")); + EXPECT_FALSE(persons.contains("bob")); +} + //TEST(MapTest, EqualityOperator) //{ // const set set1(std::set({1, 2, 3}));