如题,常见的删除方法就是用数组后面的元素依次替换,但我这么做出现了问题
原始数据库如下(测试代码,原始数据比较随性,为了贴近原本代码使用了string类型和文件流)
#include <iostream>
using namespace std;
#include <string>
#include <fstream>
#define FILE "ceshi.txt"
class ceshi{
private:
string x;
string y;
public:
void setxy(string a,string b){
x=a;
y=b;
}
void getxy(){
cout<<x<<" "<<y<<endl;
}
string getx(){
return x;
}
};
int main(int argc, char** argv) {
ceshi c[10];
static int num=0;
ifstream file;
file.open(FILE,ios::in|ios::app);
if(!file.is_open()) {
cout<<"文件打开失败"<<endl;
system("pause");
}
string n,na;
//float p,q,s;
while(!file.eof()&&num<1000){
file>>n>>na;
c[num].setxy(n,na);
num++;
file.get();
if(file.peek()=='/n') break;
}//将文档内变量写入程序
file.close();
for(int i=0;i<num;i++){
c[i].getxy();
}
string del;
cin>>del;
int jishu=0;
for(int i=0;i<num;i++){
if(c[i].getx()==del){
jishu++;
for(int j=i;j<num;j++){
c[i]=c[i+1];
}
}
}
num-=jishu;
for(int i=0;i<num;i++){
c[i].getxy();
}
return 0;
}
按照预想结果,输入a检测后删除数组第一个,应该是后四行依次输出
然而输出之后变成了这个样子
用一个普通的整数数组测试的话是没有问题的
所以怎么达到我想要的结果呢