Question
Write a c++ class `Map` that is templated on both the Key and the Value type, pretty much like a std::map. However, this class is
The add method allows keys, and values to be added in any order. All test cases will use different types for the key and value. The querying functions simply return a default value of the key/value if the corresponding value/key is missing. Unlike a std::map there is no default insertion behavior when the key/value is missing.
No loops are allowed here, you need to write these functions with standard algorithms.
Please take into consideration the test cases shown in the above pictures, as the code evaluation will be on these test cases. No main is needed.
First test case:
Map m; ASSERT_EQ(m.size(), 0); m.add(2, \"hello\"); ASSERT_EQ(m.size(), 1); m.add(3, \"bye\"); ASSERT_EQ(m.size(), 2); ASSERT_EQ(m.value(3), \"bye\"); ASSERT_EQ(m.key(\"hello\"), 2); ASSERT_EQ(m.value(4), \"\"); ASSERT_EQ(m.key(\"ho\"), 0); m.add(\"aloha\", 5); ASSERT_EQ(m.value(5), \"aloha\"); ASSERT_EQ(m.key(\"aloha\"), 5); // ***IGNORE BELOW*** // no loops check #include #include #include std::ifstream code{\"code.cpp\"}; std::regex r{R\"~(\\bfor\\b|\\bwhile\\b|\\bgoto\\b)~\"}; std::string line; while (std::getline(code, line)) ASSERT_TRUE(!std::regex_search(line,r));,>
Second test case:
Map m; ASSERT_EQ(m.size(), 0); m.add(2, \"hello\"); ASSERT_EQ(m.size(), 1); m.add(3, \"bye\"); ASSERT_EQ(m.size(), 2); ASSERT_EQ(m.key(3), \"bye\"); ASSERT_EQ(m.value(\"hello\"), 2); ASSERT_EQ(m.key(4), \"\"); ASSERT_EQ(m.value(\"ho\"), 0); m.add(\"aloha\", 5); ASSERT_EQ(m.key(5), \"aloha\"); ASSERT_EQ(m.value(\"aloha\"), 5); m.add(\"aloha\", 6); ASSERT_EQ(m.key(5), \"aloha\"); ASSERT_EQ(m.value(\"aloha\"), 5); // ***IGNORE BELOW*** // no loops check #include #include #include std::ifstream code{\"code.cpp\"}; std::regex r{R\"~(\\bfor\\b|\\bwhile\\b|\\bgoto\\b)~\"}; std::string line; while (std::getline(code, line)) ASSERT_TRUE(!std::regex_search(line,r));,>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started