删除存储在数组中的特定类对象
Deleting specific class objects stored in an array
可能的重复项:
删除存储在数组中的特定类对象
我正在尝试删除类对象数组中的特定元素。我正在用它后面的元素覆盖我要删除的元素。我的算法有效,但输出不正确,经过调试并逐步浏览代码后,似乎我的对象只是不复制,有没有办法复制类对象,我已经查找了复制构造函数并尝试编写一个,但它似乎对输出没有任何影响。我将不胜感激任何帮助。谢谢
下面是我的代码
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
void storeinfo() ;
void showinfo() ;
void menu() ;
void deleteinfo() ;
void displayallinfo() ;
int linsearch(string val) ;
class user
{
string firstname, lastname, currentteam, position, status ;
int age ;
public:
user() {};
user(string fname, string lname, string cteam, string pos, string stat, int age)
{
setFirstName(fname);
setLastName(lname);
setCurrentTeam(cteam);
setPosition(pos);
setStatus(stat);
setAge(age);
} ;
void setFirstName(string fname)
{firstname = fname;}
void setLastName(string lname)
{lastname = lname;}
void setCurrentTeam(string cteam)
{currentteam = cteam;}
void setPosition(string pos)
{position = pos;}
void setStatus(string stat)
{status = stat;}
void setAge(int _age)
{age = _age;}
string getFirstName()
{return firstname ;}
string getLastName()
{return lastname ;}
string getCurrentTeam()
{return currentteam ;}
string getPosition()
{return position ;}
string getStatus()
{return status ;}
int getAge()
{return age ;}
};
user player[20] ;
int arrlength = 3 ;
int main()
{
menu() ;
cin.get() ;
return 0 ;
}
void storeinfo()
{
string firstname ;
string lastname ;
string currentteam ;
string position;
string status ;
int age ;
for (int i=0; i < 3; i++)
{
cout << "nn Enter First Name : " ;
cin >> firstname ;
player[i].setFirstName(firstname) ;
cout << "Enter Last Name : " ;
cin >> lastname ;
player[i].setLastName(lastname) ;
cout << "Enter Player's Age : " ;
cin >> age;
player[i].setAge(age) ;
cout << "Enter Current Team : " ;
cin >> currentteam ;
player[i].setCurrentTeam(currentteam) ;
cout << "Enter Position : " ;
cin >> position ;
player[i].setPosition(position) ;
cout << "Enter Status : " ;
cin >> status ;
player[i].setStatus(status) ;
cout << "nnn" ;
}
/*cout << string(50, 'n');*/
menu() ;
}
void showinfo()
{
string search;
int found ;
cout << "Please Enter The Player's Last Name : " ;
cin >> search ;
found=linsearch(search);
if (found==-1)
{
cout << "n There is no player called " << search ;
}
else
{
cout << "n First Name : " << player[found].getFirstName() << "n" << "Last Name : " << player[found].getLastName() <<
"n" << "Age : " << player[found].getAge() << "n" << "Current Team : " << player[found].getCurrentTeam() <<
"n" << "Position : " << player[found].getPosition() << "n" << "Status : " << player[found].getStatus() << "nn";
}
cin.get() ;
menu() ;
}
void deleteinfo()
{
int arrlength = 3 ;
string search ;
int found ;
cout << "n Delete A Player's Information nn" ;
cout << "Please Enter The Player's Last Name : " ;
cin >> search ;
found=linsearch(search);
if (found==-1)
{
cout << "n There is no player called " << search ;
}
else
{
for (int i=found + 1; i < arrlength; ++i)
{
player[i-1].setFirstName(player[i].getFirstName()) ;
player[i-1].setLastName(player[i].getLastName()) ;
player[i-1].setAge(player[i].getAge()) ;
player[i-1].setCurrentTeam(player[i].getCurrentTeam()) ;
player[i-1].setPosition(player[i].getPosition()) ;
player[i-1].setStatus(player[i].getStatus()) ;
}
--arrlength ;
cout << "n Player has been deleted." ;
}
cin.get() ;
menu() ;
}
void displayallinfo()
{
for (int i=0; i < 3; i++)
{
cout << "n First Name : " << player[i].getFirstName() << "n" << "Last Name : " << player[i].getLastName() <<
"n" << "Age : " << player[i].getAge() << "n" << "Current Team : " << player[i].getCurrentTeam() <<
"n" << "Position : " << player[i].getPosition() << "n" << "Status : " << player[i].getStatus() << "nn";
}
cin.get() ;
menu() ;
}
void menu()
{
cout << "nn MENU" << "n" ;
cout << "n A. Store Player Information" ;
cout << "n B. Show Player Informaton" ;
cout << "n C. Delete Player Information" ;
cout << "n D. Display All Players";
cout << "n Z. Exit nn" ;
string x = "";
cin >> x ;
if (x=="a" | x=="A")
{
storeinfo() ;
}
else if (x=="b" | x=="B")
{
showinfo() ;
}
else if (x=="c" | x=="C")
{
deleteinfo() ;
}
else if (x=="d" | x=="D")
{
displayallinfo() ;
}
else if (x=="z" | x=="Z")
{
exit(0) ;
}
else
{
cout << "Invalid Choice" ;
menu() ;
}
}
int linsearch(string val)
{
for (int j=0; j <= 3; j++)
{
if (player[j].getLastName()==val)
return j ;
}
return -1 ;
}
>player[i-1].getFirstName()
返回实例的 firstname 成员变量的副本。为此函数的输出分配另一个值不会更改存储在实例本身中的值。请改用player[i-1].setFirstName(player[i].getFirstName())
。此外,您可以通过以下方式使用赋值操作器,而不是复制 for 循环中的每个成员 player[i-1] = player[i];
数组真的不适合这种事情。 考虑使用 std::list。 这也意味着您可以摆脱所有尴尬的大小硬编码。
相关文章:
- C++ 在堆栈中包含多态属性的类对象存储
- 将对象存储为其基本虚拟类
- 将大型对象存储在无序映射中是否效率低下
- C++:将未知类型的对象存储为成员变量的类
- 按unique_pointer将传递的对象存储在地图中
- 如何将 c++ 类对象存储为数组?
- 将对象存储在 std::map 中
- 是否存在对象存储在其生存期内可能会更改的情况?
- 将不同类型的对象存储在容器中
- 将自定义可绘制对象存储在数组中会导致绘制时出现分割错误
- 我将如何在C++中文件处理对象(存储/导入)
- 将对象存储在C 中的2维Sprite矩阵中
- 将抽象派生类对象存储在基类向量中
- 将任意函数对象存储到类成员容器中,而无需知道其声明签名
- 如何使用向量将类对象存储在超载操作员中
- 适当的方法将对象存储在Std矢量中
- 将向量对象存储在共享指针投掷错误中
- 将多态对象存储在unordered_set中
- 为什么我可以将对象存储在大小不同的数组中
- 返回对象存储在哪里