C++ 使用std::replace c+;时,二进制表达式的操作数无效+;
我有一个包含用户名和密码的向量。我试图做一个“重置新密码”功能,首先在向量中搜索用户名,看用户名是否存在, 然后,下一步将提示用户输入新密码,旧密码将替换为新密码 但是我遇到了这个错误C++ 使用std::replace c+;时,二进制表达式的操作数无效+;,c++,C++,我有一个包含用户名和密码的向量。我试图做一个“重置新密码”功能,首先在向量中搜索用户名,看用户名是否存在, 然后,下一步将提示用户输入新密码,旧密码将替换为新密码 但是我遇到了这个错误 Invalid operands to binary expression ('User' and 'const std::__1::basic_string<char>') 我的部分代码与问题有关 string name; cout << "Please enter a user na
Invalid operands to binary expression ('User' and 'const std::__1::basic_string<char>')
我的部分代码与问题有关
string name;
cout << "Please enter a user name that the password will be reset \n";
cin >> name;
for (int i =0; i<userDetails.size(); i++) {
if (userDetails[i].getUserName() == name) {
string newPassword;
string oldPassword;
oldPassword = userDetails[i].getPassword();
cout << "Please enter a new password" << endl;
cin >> newPassword;
replace(userDetails.begin(), userDetails.end(), oldPassword, newPassword);
cout << userDetails[i].getPassword();
}
}
字符串名;
姓名;
对于(int i=0;i而言,您的userDetails
容器似乎包含User
对象,但您将其视为包含std::string
s。您应该替换userDetails[i]
的密码。如何操作取决于User
类的详细信息
您可能需要这样的内容,与适当的谓词一起使用,以查找用户名等于name
的第一个用户:
// find the right User
auto it = std::find_if(userDetails.begin(),
userDetails.end(),
[&name](const User& user)
{ return user.getUserName() == name;})
然后替换该用户的密码:
if (it != userDetails.end()) {
// we found a user with username == name
it->setPasswd(newPasswd);
} else
// no user found with that username
std::cout << "User " << name << " not found" << std::endl;
}
if(it!=userDetails.end()){
//我们找到一个username==name的用户
it->setPasswd(newPasswd);
}否则
//找不到具有该用户名的用户
std::cout您的userDetails
容器似乎包含User
对象,但您将其视为包含std::string
s。您应该替换userDetails[i]
的密码。如何操作取决于User
类的详细信息
您可能需要这样的内容,与适当的谓词一起使用,以查找用户名等于name
的第一个用户:
// find the right User
auto it = std::find_if(userDetails.begin(),
userDetails.end(),
[&name](const User& user)
{ return user.getUserName() == name;})
然后替换该用户的密码:
if (it != userDetails.end()) {
// we found a user with username == name
it->setPasswd(newPasswd);
} else
// no user found with that username
std::cout << "User " << name << " not found" << std::endl;
}
if(it!=userDetails.end()){
//我们找到一个username==name的用户
it->setPasswd(newPasswd);
}否则
//找不到具有该用户名的用户
std::cout您的userDetails
容器似乎包含User
对象,但您将其视为包含std::string
s。您应该替换userDetails[i]
的密码。如何操作取决于User
类的详细信息
您可能需要这样的内容,与适当的谓词一起使用,以查找用户名等于name
的第一个用户:
// find the right User
auto it = std::find_if(userDetails.begin(),
userDetails.end(),
[&name](const User& user)
{ return user.getUserName() == name;})
然后替换该用户的密码:
if (it != userDetails.end()) {
// we found a user with username == name
it->setPasswd(newPasswd);
} else
// no user found with that username
std::cout << "User " << name << " not found" << std::endl;
}
if(it!=userDetails.end()){
//我们找到一个username==name的用户
it->setPasswd(newPasswd);
}否则
//找不到具有该用户名的用户
std::cout您的userDetails
容器似乎包含User
对象,但您将其视为包含std::string
s。您应该替换userDetails[i]
的密码。如何操作取决于User
类的详细信息
您可能需要这样的内容,与适当的谓词一起使用,以查找用户名等于name
的第一个用户:
// find the right User
auto it = std::find_if(userDetails.begin(),
userDetails.end(),
[&name](const User& user)
{ return user.getUserName() == name;})
然后替换该用户的密码:
if (it != userDetails.end()) {
// we found a user with username == name
it->setPasswd(newPasswd);
} else
// no user found with that username
std::cout << "User " << name << " not found" << std::endl;
}
if(it!=userDetails.end()){
//我们找到一个username==name的用户
it->setPasswd(newPasswd);
}否则
//找不到具有该用户名的用户
std::cout第一个问题是
replace(userDetails.begin(), userDetails.end(), oldPassword, newPassword);
您正试图用std::string
对象替换User
对象
第二个问题(如n.m.所述)-当您已经掌握了正确的用户
对象时,不需要在循环中使用基于范围的函数(replace()
或其他函数)
您可以使用std::find_if()
函数查找具有正确名称的User
对象,而不是编写循环。在C++11中,可能是这样的:
auto user = find_if(userDetails.begin(), userDetails.end(),
[&name](const User& u){return u.getUserName()==name;});
在C++11之前的代码中,您需要一个单独的谓词函数,而不是lambda/您自己的搜索循环(当找到用户时,只需break;
)
如果找到用户,您可以设置密码:
if(user != userDetails.end()){
// (ask for the new password here)
user->setPassword(newPassword); // assuming you have a setter for password field
}
第一个问题就在眼前
replace(userDetails.begin(), userDetails.end(), oldPassword, newPassword);
您正试图用std::string
对象替换User
对象
第二个问题(如n.m.所述)-当您已经掌握了正确的用户
对象时,不需要在循环中使用基于范围的函数(replace()
或其他函数)
您可以使用std::find_if()
函数查找具有正确名称的User
对象,而不是编写循环。在C++11中,可能是这样的:
auto user = find_if(userDetails.begin(), userDetails.end(),
[&name](const User& u){return u.getUserName()==name;});
在C++11之前的代码中,您需要一个单独的谓词函数,而不是lambda/您自己的搜索循环(当找到用户时,只需break;
)
如果找到用户,您可以设置密码:
if(user != userDetails.end()){
// (ask for the new password here)
user->setPassword(newPassword); // assuming you have a setter for password field
}
第一个问题就在眼前
replace(userDetails.begin(), userDetails.end(), oldPassword, newPassword);
您正试图用std::string
对象替换User
对象
第二个问题(如n.m.所述)-当您已经掌握了正确的用户
对象时,不需要在循环中使用基于范围的函数(replace()
或其他函数)
您可以使用std::find_if()
函数查找具有正确名称的User
对象,而不是编写循环。在C++11中,可能是这样的:
auto user = find_if(userDetails.begin(), userDetails.end(),
[&name](const User& u){return u.getUserName()==name;});
在C++11之前的代码中,您需要一个单独的谓词函数,而不是lambda/您自己的搜索循环(当找到用户时,只需break;
)
如果找到用户,您可以设置密码:
if(user != userDetails.end()){
// (ask for the new password here)
user->setPassword(newPassword); // assuming you have a setter for password field
}
第一个问题就在眼前
replace(userDetails.begin(), userDetails.end(), oldPassword, newPassword);
您正试图用std::string
对象替换User
对象
第二个问题(如n.m.所述)-当您已经掌握了正确的用户
对象时,不需要在循环中使用基于范围的函数(replace()
或其他函数)
您可以使用std::find_if()
func