You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++数组元素移除问题:禁用vector时无法正确前移元素

数组元素移除问题:删除后最后一个元素重复,无法正确前移

我在做学校项目,要实现数组元素移除功能,禁止使用vector。现在能删除目标元素(A3),但运行后最后一个元素重复,后续元素没整体前移。要求函数只能以studentID作为输入。

错误输出

Student ID   First Name  Last Name   Email Address   Age     Days In Courses     Degree Program
A1  John    Smith   John1989@gmail.com 20  30,35,40    Security
A2  Suzan   Erickson    Erickson_1990@gmailcom  19  50,30,40    Network
A5  Dyllan  Hackett dhack27@gmail.com   30  14,4,25 Software
A4  Erin    Black   Erin.black@comcast.net  22  50,58,40    Security
A5  Dyllan  Hackett dhack27@gmail.com   30  14,4,25 Software

期望输出

Student ID   First Name  Last Name   Email Address   Age     Days In Courses     Degree Program
A1  John    Smith   John1989@gmail.com 20  30,35,40    Security
A2  Suzan   Erickson    Erickson_1990@gmailcom  19  50,30,40    Network
A4  Erin    Black   Erin.black@comcast.net  22  50,58,40    Security
A5  Dyllan  Hackett dhack27@gmail.com   30  14,4,25 Software

现有错误代码

//
//  roster.cpp
//  DyllanHackett_C867
//
//  Created by Dyllan Hackett on 4/28/23.

#include <iostream>
#include <string>
#include <vector>
#include <regex>
#include "roster.h"
#include "student.h"
#include "degree.h"

using namespace std;


// Remove student_ID
void Roster::remove(string studentID){
    int j;
    int found = 0;
    int tot = 5;
    for (int i = 0; i < 5; i++) {
        if (classRosterArray[i]->getStudentID() == studentID) {
            for (j=i; j <(tot -1);j++) {
                classRosterArray[j] = classRosterArray[j + i];
                found++;
                i--;
                tot--;
            }
        }
    }
    if (found == 0)
        cout << "StudentID not found in roster.\n";
    else
            cout << "StudentID removed.\n";
    cout << endl;
}

问题分析与修正方案

你的remove函数存在三个核心逻辑错误:

  1. 元素前移时用了j+i而非j+1,导致跳过大量元素,错误复制非相邻元素
  2. 嵌套循环中修改外层循环的i和tot,导致循环逻辑混乱,无法正确遍历
  3. 未处理数组末尾的无效元素,导致重复输出

修正后的代码

void Roster::remove(string studentID){
    int foundIndex = -1;
    // 第一步:遍历找到目标元素的索引
    for (int i = 0; i < 5; i++) {
        // 先判断指针非空,避免空指针访问
        if (classRosterArray[i] != nullptr && classRosterArray[i]->getStudentID() == studentID) {
            foundIndex = i;
            break;
        }
    }

    // 未找到目标ID的情况
    if (foundIndex == -1) {
        cout << "StudentID not found in roster.\n";
        cout << endl;
        return;
    }

    // 第二步:将目标位置后的元素整体前移一位
    for (int j = foundIndex; j < 4; j++) {
        classRosterArray[j] = classRosterArray[j + 1];
    }

    // 第三步:将最后一个位置置空,标记为无效
    classRosterArray[4] = nullptr;

    cout << "StudentID removed.\n";
    cout << endl;
}

关键改动说明

  • 拆分逻辑:先单独遍历找到目标索引,再执行前移操作,避免嵌套循环的逻辑混乱
  • 正确前移:使用j+1确保每个后续元素都往前挪一位,覆盖被删除的位置
  • 标记无效:将数组最后一个位置设为nullptr,需确保输出函数打印前会判断指针是否非空,跳过无效元素
  • 空指针检查:避免访问已置空的数组元素导致程序崩溃

内容的提问来源于stack exchange,提问作者Dyl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 22:57:07