gpt4 book ai didi

c++ - 搜索类对象数组 C++

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:58:17 25 4
gpt4 key购买 nike

我需要帮助来尝试搜索类对象数组。我有一个名为 users 的类,并且有一个数组,其中存储了 3 个玩家。我希望能够在输入他的两个名字时显示特定玩家的信息,并在输入名字时删除他的记录。

我知道我可以使用 vector 列表来简化此操作,但我设置了限制。我也认为可以实现线性搜索,但我不知道这是否足够有效。

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

void storeinfo() ;
void showinfo() ;
void menu() ;


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 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 << "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 << "\n\n\n" ;
}

/*cout << string(50, '\n');*/

menu() ;

}

void showinfo()
{
for (int i=0; i < 3; i++)
{
cout << "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() << "\n\n";
}

cin.get() ;

menu() ;
}

void menu()
{
cout << "\n MENU" << "\n" ;
cout << "\n 1. Store Player Information" ;
cout << "\n 2. Show Player Informaton" ;
cout << "\n 0. Exit \n \n" ;

string x = "";
cin >> x ;

if (x=="a")
{
storeinfo() ;
}
else if (x=="b")
{
showinfo() ;
}
else if (x=="c")
{
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;
}
else
{
return 1;
}
}




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() << "\n\n";
}

cin.get() ;

menu() ;

}

最佳答案

如果您的数据集可以变大,哈希表是您想到的解决方案。一定要为你的任务选择一个没有太多冲突的散列算法。顺便说一下,有“perfect hashing algorithms”——如果“始终零碰撞”很重要,你应该看看它——无碰撞、快速的哈希表将显着加快速度

关于c++ - 搜索类对象数组 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14466723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com