遍历类向量导致问题
Iterating through a class vector causing problems
我的程序正在尝试创建一个新帐户,并将用户名与数据库中的用户名进行比较。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
,如另一个答案
相关文章:
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- 添加存储在向量中的大整数的函数出现问题
- 尝试将向量初始化为类成员时出现问题
- 在将字符串放入字符串向量时遇到问题?
- 取消引用向量时出现问题
- 在C++中对结构向量进行排序时出现问题
- 在 c++ 中创建和访问 4D 向量时出现问题
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- 使用指针向量(带代码)C++(以及对其使用的便利性的怀疑)时出现问题
- C++ 继承向量问题(无限循环+在其他类中使用向量的问题)
- 在 c++ 中访问列表向量时出现问题
- c++向量push_back问题
- shared_ptr类的向量的向量初始化问题
- SWIG c++到python:向量问题
- 在 Rcpp-Function 中使用布尔向量的子集(Rcpp 初学者的问题..)
- 无法取消引用超出范围的向量迭代器 - 有什么问题?
- 在构造函数中更新指针向量时出现问题
- 在使用 std::sort() 和 lambda 函数按属性对 ADT 的向量进行排序时遇到问题
- 为什么C++在自定义结构向量时会出现 ostream 重载问题?
- C++:问题向量 STL.