通过cin比较C ++中的两个数组

时间:2014-05-13 02:59:38

标签: c++ arrays

好的我尝试做这样的事情

string account[3]={"asr123","cbg567","oit777"};
int  pins[3]={1234,4567,7890};

string userAccount;
int userPin;

cin >> userAccount;
cin >> userPin;

现在如何制作帐号" asr123"和别针" 1234"和#34;在一起" 1234是那个账号的针脚?任何提示,我尝试使用lop但任何这些引脚

3 个答案:

答案 0 :(得分:2)

您应该使用地图,这将很容易编码和有效。

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

答案 1 :(得分:0)

如果要查找具有特定ID的字符串,可以使用std :: map。您可以稍后逐个添加对(在您的情况下为3对),然后使用整数轻松查看您的字符串。

答案 2 :(得分:0)

听起来你想确保用户输入的两个值与两个数组中的两个值匹配。

线性时间解决方案只是循环遍历两个阵列,尽管这不是检查用户登录的好方法

你会写下面的内容。

for (int i = 0; i < 3; i++) {
    if (userAccount.compare(account[i]) == 0 && userPin == pins[i]) {
        // Do something because there was a match.
    }
}