Introduction

Welcome! Today, we are going to explore an engaging task that involves managing employee records within a company. Specifically, we will work with nested std::map and std::vector to add projects and tasks for employees and retrieve those tasks as needed. This exercise will help you understand how to manipulate nested data structures efficiently in C++.

Introducing Methods to Implement

Let's start by discussing the methods we will implement in our EmployeeRecords class.

  • bool add_project(const std::string& employee_id, const std::string& project_name) - this method adds a new project to an employee's list of projects. If the project already exists for that employee, the method returns false. Otherwise, it adds the project and returns true.
  • bool add_task(const std::string& employee_id, const std::string& project_name, const std::string& task) - this method adds a new task to a specified project for an employee. If the project does not exist for that employee, the method returns false. If the task is added successfully, it returns true.
  • std::vector<std::string> get_tasks(const std::string& employee_id, const std::string& project_name) - this method retrieves all tasks for a specified project of an employee. If the project does not exist for that employee, the method returns an empty vector. Otherwise, it returns the list of tasks.
Step 1: Basic Class Structure

Now, let's build our EmployeeRecords class step by step, ensuring we understand each component clearly.

We'll start with the basic structure of the class and initialize our data storage.

C++
#include <iostream>
#include <map>
#include <vector>
#include <string>

class EmployeeRecords {
public:
    EmployeeRecords() = default;

private:
    std::map<std::string, std::map<std::string, std::vector<std::string>>> records;
};

// Instantiate the class to ensure it works
int main() {
    EmployeeRecords records;
    return 0;
}

In this initial setup, we define the EmployeeRecords class and create an instance variable records that is a std::map. This map will be used to store employee records, where each key is an employee ID and each value is another map holding projects.

Step 2: Implementing `add_project` Method
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal