1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#include <iostream> #include <vector> #include <unordered_map> #include <string> class DataWarehouse { public: // Adds data to the warehouse void addData(const std::string& key, const std::string& value) { data[key] = value; } // Retrieves data from the warehouse std::string getData(const std::string& key) const { auto it = data.find(key); if (it != data.end()) { return it->second; } else { return "Data not found"; } } // Displays all data in the warehouse void displayAllData() const { for (const auto& entry : data) { std::cout << "Key: " << entry.first << ", Value: " << entry.second << std::endl; } } private: std::unordered_map<std::string, std::string> data; }; int main() { DataWarehouse warehouse; // Add some data to the warehouse warehouse.addData("User1", "Alice"); warehouse.addData("User2", "Bob"); warehouse.addData("User3", "Charlie"); // Retrieve and display specific data std::cout << "Retrieving User2: " << warehouse.getData("User2") << std::endl; // Display all data std::cout << "Displaying all data:" << std::endl; warehouse.displayAllData(); return 0; } |
Explanation
- Header Files:
<iostream>
: For input and output operations.<vector>
: Although not used in this example, it’s included for potential extensions.<unordered_map>
: To store key-value pairs efficiently.<string>
: For handling string operations.
Advertisement - DataWarehouse Class:
- addData(): Adds a key-value pair to the data warehouse. The
key
is a unique identifier, and thevalue
is the data associated with that key. - getData(): Retrieves the value associated with a given key. If the key is not found, it returns “Data not found”.
- displayAllData(): Displays all key-value pairs stored in the data warehouse.
- addData(): Adds a key-value pair to the data warehouse. The
- main() Function:
- Creates an instance of
DataWarehouse
Advertisement - Adds some key-value pairs to the data warehouse.
- Retrieves and displays a specific data entry using the
getData()
method. - Displays all data entries using the
displayAllData()
method.
- Creates an instance of