map of vector of struct error

#include <iostream>
#include <vector>
#include <map>

struct Data {
    int id;
    std::string name;
};

int main() {
    std::map<int, std::vector<Data>> myMap;

    Data d1 = {1, "Alice"};
    Data d2 = {2, "Bob"};
    Data d3 = {3, "Charlie"};

    myMap[101].push_back(d1);
    myMap[101].push_back(d2);
    myMap[102].push_back(d3);

    for (const auto& pair : myMap) {
        std::cout << "Key: " << pair.first << std::endl;
        for (const auto& data : pair.second) {
            std::cout << "ID: " << data.id << ", Name: " << data.name << std::endl;
        }
    }

    return 0;
}

Explanation:

  1. Include Libraries: Include necessary libraries for input/output, vectors, and maps.

  2. Define a Struct: Create a struct named Data with two members: id of type integer and name of type string.

  3. Main Function: The program's entry point.

  4. Declare a Map of Vectors of Structs: Define a map named myMap where the keys are integers and the values are vectors of Data structs.

  5. Create Data Instances: Define three Data instances named d1, d2, and d3, each containing an ID and a name.

  6. Insert Data into the Map: Add d1 and d2 into myMap with key 101, and d3 into myMap with key 102. This creates vectors associated with keys 101 and 102 containing the respective Data elements.

  7. Iterate Through the Map: Use a for loop to iterate through each key-value pair in myMap.

  8. Print Key: Print the current key from the map.

  9. Print Data within the Vectors: Use another loop to iterate through each Data element within the vector associated with the current key and print its ID and name.

  10. Return: End the program, returning 0 to indicate successful completion.