遍历类向量导致问题

Iterating through a class vector causing problems

本文关键字:问题 向量 遍历      更新时间:2023-10-16

我的程序正在尝试创建一个新帐户,并将用户名与数据库中的用户名进行比较。1来自用户输入,另一个来自矢量。我必须遍历向量并比较这两个字符串。但由于某种未知的原因,它只比较FIRST值,而不是比较矢量内的所有值。

这是我的代码:
我的登录Ac.cpp

class AccountInfo {
public:
string username;
string password;
string type;
}; 
bool LoginAcc::checkAccountNameValid(string username) {
vector <AccountInfo> accInfo;
AccountInfo user;
ifstream UserDatabase("UserDatabase.txt");
string line = "";
while (getline(UserDatabase,line)) {
    stringstream linestream(line);
    getline(linestream,user.username,':');
    accInfo.push_back(user);
}
UserDatabase.close();
for(vector<AccountInfo>::iterator itr = accInfo.begin(); itr != accInfo.end(); ++itr) {
    if (username.compare((*itr).username) != 0)
        return true;
    else 
        return false;
}

我的主.cpp

        case 'n':
        while (!flag) {
            cout << "Please enter a username with 8 characters. " << endl;
            cout << "Username : ";
            cin >> username;
            if (username.length() != 8) {
                cout << "Username does not meet the requirements" << endl;
                cout << "Username : ";
                cin >> username;
            }
            else {
                valid = login.checkAccountNameValid(username);
                if (valid == true) {
                    cout << "Please enter a password with 8 characters." << endl;
                    cout << "Password : " << endl;
                    cin >> password;
                    cout << "1. Student" << endl;
                    cout << "2. Lecturer" << endl;
                    cout << "3. Admin" << endl;
                    cout << "Please choose the category you belong to : ";
                    cin >> category;
                    login.createNewAcc(username,password,category);
                }
                else {
                    cout << "Username has been taken. Please choose another. " << endl;
                    cout << "Username : ";
                    cin >> username;
                }
            }
        }
        break;

我觉得我的"checkAccountNameValid"方法中的逻辑有问题。有人能提出建议吗?谢谢

无论结果如何,内部循环都会返回给调用者。您应该在循环完成后return false

另外,不要使用username.compare((*itr).username) != 0。这很令人困惑(可能是你为什么弄错了(它应该是== 0)),你可以只使用等式运算符:username == itr->username,它是为std::string定义的(如果你担心整体,"不要用等式比较char *!")。

但你真正应该做的是将数据存储在一个键入用户名的地图中,并进行直接查找:

std::map<std::string, AccountInfo> accInfo;
⋮
return accInfo.find(username) != accInfo.end();
for(vector<AccountInfo>::iterator itr = accInfo.begin(); itr != accInfo.end(); ++itr) 
{
    if (username.compare((*itr).username) != 0)
        return true;
}
return false;

等待返回CCD_ 7。

我还认为您可能已将!= 0反转:http://en.cppreference.com/w/cpp/string/basic_string/compare.

另一种排列代码的方法可能会给你更多关于它是如何工作的线索:

bool found = false;
for(auto itr = accInfo.begin(); itr != accInfo.end(); ++itr) 
{
    if (username.compare(itr->username) == 0)
        found = true;
}
return found;

一种更现代的方法是

bool found = std::find_if(accInfo.begin(), accInfo.end(), 
   [] (AccountInfo const& ai) { return username == ai.username; });

或者实际上,使用一个便于查找的数据结构,如std::map,如另一个答案

所示