Mastering Data Aggregation and Data Streams handling with C++

Introduction

Welcome to our lesson on mastering data aggregation and data streams with C++. In this lesson, you'll learn to build a basic sales records aggregator using C++'s standard library containers. Then, we'll extend its functionality to handle more complex operations such as filtering, data aggregation, and formatting. By the end of this session, you'll be proficient in managing and formatting data streams efficiently in C++.

Starter Task Methods and Their Definitions

To get started, we'll create a simple sales record aggregator in C++. Here are the methods we'll focus on:

  • void add_sale(const std::string& sale_id, double amount, const std::string& date); - Adds or updates a sale record with a unique identifier sale_id, amount, and a date in the format "YYYY-MM-DD".

  • std::optional<double> get_sale(const std::string& sale_id) const; - Retrieves the sale amount associated with the sale_id. If the sale does not exist, it returns an empty optional.

  • bool delete_sale(const std::string& sale_id); - Deletes the sale record with the given sale_id. Returns true if the sale was deleted and false if the sale does not exist.

Are these methods clear so far? Great! Let's now look at how we would implement them.

Starter Task Implementation

Here is the complete code for the starter task:

#include <iostream>
#include <map>
#include <optional>
#include <string>

class SalesAggregator {
public:
    void add_sale(const std::string& sale_id, double amount, const std::string& date) {
        sales[sale_id] = {amount, date};
    }

    std::optional<double> get_sale(const std::string& sale_id) const {
        auto it = sales.find(sale_id);
        if (it != sales.end()) {
            return it->second.first;
        }
        return std::nullopt;
    }

    bool delete_sale(const std::string& sale_id) {
        return sales.erase(sale_id) > 0;
    }

private:
    std::map<std::string, std::pair<double, std::string>> sales;
};

// Example Usage
int main() {
    SalesAggregator aggregator;

    // Add sales
    aggregator.add_sale("001", 100.50, "2023-01-01");
    aggregator.add_sale("002", 200.75, "2023-01-15");

    // Get sale
    if (auto sale = aggregator.get_sale("001")) {
        std::cout << *sale << std::endl; // Output: 100.5
    }

    // Delete sale
    std::cout << std::boolalpha << aggregator.delete_sale("002") << std::endl; // Output: true
    if (auto sale = aggregator.get_sale("002")) {
        std::cout << *sale << std::endl;
    } else {
        std::cout << "Sale not found" << std::endl; // Output: Sale not found
    }

    return 0;
}

Explanation:

  • The sales map stores sale records with sale_id as the key and a pair of amount and date as the value.
  • add_sale adds a new sale or updates an existing sale ID.
  • get_sale retrieves the amount for a given sale ID or returns std::nullopt if the sale does not exist.
  • delete_sale removes the sale record for the given sale ID or returns false if the sale does not exist.

Now that we have our basic aggregator, let's extend it to include more advanced functionalities.

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