通过cin比较c++中的两个数组
Comparing two arrays in C++ through cin
我试着这样做
string account[3]={"asr123","cbg567","oit777"};
int pins[3]={1234,4567,7890};
string userAccount;
int userPin;
cin >> userAccount;
cin >> userPin;
现在如何使帐户"asr123"和pin"1234"在一起",即1234是该帐户的pin ?有什么提示吗,我试过了但是这些引脚
您应该使用map来代替,它将易于编码且高效。
map<string, int> passwords;
passwords["asr123"]=12345; // do like this for all pairs
string userAccount;
int userPin;
cin >> userAccount;
cin >> userPin;
if(passwords[userAccount]==userPin){
cout<<"OK";
}
else{
cout<<"incorrect";
}
如果要查找具有特定ID的字符串,可以使用std::map。您可以稍后逐个添加字符串对(在您的示例中为3对),然后轻松地使用int查找字符串。
听起来您想要确保用户输入的两个值与您的两个数组中的两个值匹配。
线性时间的解决方案是循环遍历这两个数组,尽管这不是检查用户登录的好方法。你可以这样写:
for (int i = 0; i < 3; i++) {
if (userAccount.compare(account[i]) == 0 && userPin == pins[i]) {
// Do something because there was a match.
}
}
相关文章:
- 将fold表达式与std::一起用于两个元组
- 如何在C++中比较两个char数组
- 在c++中获取两个大int,并将它们存储在数组中
- 如何以优化的方式同时迭代两个间距不相等的数组
- 运算符重载 (+),用于添加两个具有 C++ 的数组
- 将字符串拆分为标记,并将标记拆分为两个单独的数组
- 可以将两个相同类型的连续数组视为一个数组吗?
- C++递归来决定数组中的两个值
- 如何将字符串和整数读取到两个单独的动态数组中的程序编写?
- 按 Tab 单步执行两个控制组
- 如何检查两个元组的所有成员是否不同
- C 通过随机选择其元素从两个元组制成元组
- 如何将两个元组作为C 的参数发送
- 对于同一循环的这两个连续组,是否有更有效的解决方案
- 在 C++11 中加入两个元组
- 将两个双数组集组合为一个查找其并集和交集的最简单方法
- 如何创建一个在 C++11 (STL) 中压缩两个元组的函数
- 快速实现两个 8 位数组的协方差
- 如何绑定两个元组
- 将一个16位整数复制到一个两字节数组