Implementing Student-Course Mapping with C++ Vector Containers

Course Registration Query Using Vector Arrays

When handling dynamic data where the number of items per entity varies, std::vector provides an ideal solution. Consider a scenario where we need to maintain course enrollment records and retrieve a specific student's course list on demand.

Problem Analysis

The input provides course information including course IDs and enrolled student names. The task requires outputting the sorted course list for queried students. Since each student may enroll in a different number of courses, a fixed-size array is insufficient.

The solution employs an array of vectors where each element corresponds to a student and stores their enrolled course IDs. To use student names as array indices, we need a hash function to convert names to integers.

Hash Function Design

Student names follow a fixed pattern: three uppercase letters followed by one digit. We can compute a unique integer by treating the letters as a base-26 number:

#include <cstdio>
#include <vector>
#include <algorithm>

const int MAX_STUDENTS = 26 * 26 * 26 * 10 + 10;
std::vector<int> courseList[MAX_STUDENTS];

int computeHash(const char* name) {
    int result = 0;
    for (int i = 0; i < 3; ++i) {
        result = result * 26 + (name[i] - 'A');
    }
    return result * 10 + (name[3] - '0');
}

Complete Implementation

int main() {
    int queryCount, totalCourses;
    
    while (scanf("%d %d", &queryCount, &totalCourses) == 2) {
        for (int i = 0; i < MAX_STUDENTS; ++i) {
            courseList[i].clear();
        }
        
        int courseId, studentNum;
        char studentName[5];
        
        for (int c = 0; c < totalCourses; ++c) {
            scanf("%d %d", &courseId, &studentNum);
            for (int s = 0; s < studentNum; ++s) {
                scanf("%s", studentName);
                int idx = computeHash(studentName);
                courseList[idx].push_back(courseId);
            }
        }
        
        for (int q = 0; q < queryCount; ++q) {
            scanf("%s", studentName);
            int idx = computeHash(studentName);
            
            std::sort(courseList[idx].begin(), courseList[idx].end());
            printf("%s %lu", studentName, courseList[idx].size());
            
            for (size_t i = 0; i < courseList[idx].size(); ++i) {
                printf(" %d", courseList[idx][i]);
            }
            printf("\n");
        }
    }
    return 0;
}

Course Roster Generation Using Vector of Strings

For the inverse operation—generating student lists per course—we can directly use course IDs as array indices since they are integers.

Problem Requirements

Given student enrollment data, output each course's student roster sorted alphabetically.

Solution Approach

Define a vector of strings for each course. Since course IDs are integers, no hash conversion is needed.

#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

int main() {
    int numStudents, numCourses;
    
    while (std::cin >> numStudents >> numCourses) {
        std::vector<std::string> roster[2505];
        
        std::string name;
        int courseCount, courseId;
        
        for (int s = 0; s < numStudents; ++s) {
            std::cin >> name >> courseCount;
            for (int c = 0; c < courseCount; ++c) {
                scanf("%d", &courseId);
                roster[courseId].push_back(name);
            }
        }
        
        for (int c = 1; c <= numCourses; ++c) {
            std::sort(roster[c].begin(), roster[c].end());
            printf("%d %zu\n", c, roster[c].size());
            
            for (const auto& student : roster[c]) {
                std::cout << student << "\n";
            }
        }
    }
    return 0;
}

Key Implementation Notes

When storing strings in vectors, use std::vector<std::string> rather than std::vector<char*>. The pointer variant stores memory addresses that may all reference the same buffer, leading to duplicate or incorrect values in the output. Arrays of fixed-size character arrays like std::vector<char[5]> are syntactically invalid in C++.

Tags: C++ vector algorithm data-structures Hashing

Posted on Mon, 03 Aug 2026 16:44:23 +0000 by vaanil