LearnFrenzy
Sign in Take a free test

Modern C++ Features (C++11 to C++23) · Exercise 4 of 4

Modern C++ Features (C++11 to C++23) Modern C++ Programs

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Programs written the modern way, using C++17, C++20 and C++23 features for real tasks. Write each one yourself first, then open the solution – every solution was compiled and run, and the output shown is the real output.

Question 1 Medium

Parse lines like "port = 8080" with std::string_view (trimming spaces, skipping # comments) into a map, and write get_int(key) returning std::optional<int>.

Question page & discussion
Question 2 Hard

Define a Value that can be null, bool, number, string or a list of Values using std::variant, and write a recursive to_string with std::visit.

Question page & discussion
Question 3 Medium

Parse "item,qty,price" CSV lines with string_view and from_chars into structs using designated initialisers, then print an aligned bill with std::format.

Question page & discussion
Question 4 Hard

Validate a username, age and email with functions returning std::expected<T, std::string>, combine them with and_then into a User, and report the first error.

Question page & discussion
Question 5 Medium

Store Circle, Rect and Triangle structs (no base class) in a vector of std::variant, and compute area and perimeter with std::visit and a generic lambda that calls member functions.

Question page & discussion
Question 6 Easy

Count word frequencies in a text using std::string_view splitting, a std::map with structured bindings, and print the top three with std::format, using ranges sort with a projection.

Question page & discussion
Question 7 Hard

Build a 16-entry lookup table at compile time with a consteval function returning std::array, verify it with static_assert, and use it at run time to compute a simple 4-bit-step checksum of strings.

Question page & discussion
Question 8 Easy

Define a ServerSettings struct with default member values and create several configurations using C++20 designated initialisers, then print each with std::format, using enum class and using enum for the log level.

Question page & discussion
Question 9 Medium

Write a PropertyBag storing std::any values by name with a template get<T>(name) returning std::optional<T> (empty if missing or the wrong type); store and read ints, strings and doubles.

Question page & discussion
Question 10 Medium

Rewrite an old-style "find the employee with the highest salary per department" program (raw arrays, NULL, index loops, printf) in modern C++: std::vector of structs, std::map, structured bindings, optional and std::format.

Question page & discussion
All exercises doneBack to Modern C++ Features (C++11 to C++23)Revise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links