C++比较两个数组

Compare two arrays C++

本文关键字:两个 数组 比较 C++      更新时间:2023-10-16

我正在尝试创建一个使用动态分配数组而不是向量的函数,因为我想看看它们是如何工作的。基本上,此功能要求用户输入他们将要输入的人数,然后输入他们的名字;然后,他们输入这些人中有多少人想要注册 ID,后跟他们的电话 #。

例如:

"How many customers will you like to enter? " 3 //user inputs 3
Bob Allen //user input
Ellen Michaels //user input
Jane Andrews //user input
"How many people want to register for the VIP ID? " 4 //user inputs 4; user can enter a new name here if they forgot to enter it the first time
Jane Andrews 213-2312
Bob Allen 111-1212
Jonathan Drew 211-9283
Michael Jacks 912-3911
//what the program spits out after the function is implemented
Final Registration: Guaranteed VIP IDs 
Bob Allen 111-1212
Ellen Michaels NOT GUARANTEED
Jane Andrews 213-2312
//The new users entered in the 2nd round will not be listed in the final registration because it is too late for them to sign up

基本上,我遇到的问题是:

1. How to check if the person is not listed in the second round
2. Ignore the new people the user entered in the second round
3. Print the final registration in the same order the user entered the names in the first round

这就是我现在所拥有的,但这只有在第一次和第二次输入相同数量的用户时才有效。它不检查是否有新的或省略的

string* people;
cout << "How many customers will you like to enter? " << endl;
int howmany;
cin >> howmany;
people = new int[howmany];
for (int i=0;i<howmany;i++)
{
    cin >> people[i];
}
cout << "How many people want to register for the VIP ID? " << endl;
int num_of_register;
string* vip;
cin >> num_of_register;
vip = new int[num_of_register];
for (int i=0;i<num_of_register)
{
    cin >> vip[i];
    for (int j=0;j<howmany;j++)
    {
        if (num_of_register == howmany) {
            if people[j] == vip[i] //check if they're the same people or not
                  cout << "Final Registration: Guaranteed VIP Ids" << endl;
                  cout << people[i]; 
        else {
            //check if the names are the same
        }
     }
}

任何指南/建议都会有所帮助。我不知道如何处理这个问题。谢谢!

首先,你得到多少并为人们分配内存。在第二轮中,用户有可能输入新人。您可以先获取客户数量,然后获取他们的名字,然后在第二部分中检查第一部分中是否存在该名称

cin >> num_of_register;
if( num_of_register > howmany )
{
    cout << "You can't register new peoplen";
}
for (int i=0;i<num_of_register; i++)
{
    cin >> vip[i];
    int customerExists = -1;
    for (int j=0;j<howmany;j++)
    {   
        if( people[j] == vip[i] )
        {
            customerExists = i;
            break;
        }
    }
    if(customerExists == -1)
    {
        cout << "You can not enter new customern";
        // get people[i] again
        i--;
    }
    else
    {
        cout << "Final Registration: Guaranteed VIP Ids" << endl;
        cout << people[customerExists]; 
    }
}