C++比较两个数组
Compare two arrays C++
我正在尝试创建一个使用动态分配数组而不是向量的函数,因为我想看看它们是如何工作的。基本上,此功能要求用户输入他们将要输入的人数,然后输入他们的名字;然后,他们输入这些人中有多少人想要注册 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];
}
}
相关文章:
- 将fold表达式与std::一起用于两个元组
- 如何在C++中比较两个char数组
- 在c++中获取两个大int,并将它们存储在数组中
- 如何以优化的方式同时迭代两个间距不相等的数组
- 运算符重载 (+),用于添加两个具有 C++ 的数组
- 将字符串拆分为标记,并将标记拆分为两个单独的数组
- 可以将两个相同类型的连续数组视为一个数组吗?
- C++递归来决定数组中的两个值
- 如何将字符串和整数读取到两个单独的动态数组中的程序编写?
- 按 Tab 单步执行两个控制组
- 如何检查两个元组的所有成员是否不同
- C 通过随机选择其元素从两个元组制成元组
- 如何将两个元组作为C 的参数发送
- 对于同一循环的这两个连续组,是否有更有效的解决方案
- 在 C++11 中加入两个元组
- 将两个双数组集组合为一个查找其并集和交集的最简单方法
- 如何创建一个在 C++11 (STL) 中压缩两个元组的函数
- 快速实现两个 8 位数组的协方差
- 如何绑定两个元组
- 将一个16位整数复制到一个两字节数组