This is in C++. If you can show me a different way to write this and comment wah
ID: 3869052 • Letter: T
Question
This is in C++. If you can show me a different way to write this and comment waht it does. Thank you.
void search(const std::string& fsearch, const std::string& lsearch, const PeopleList& p1, PeopleList& result) {
if (fsearch == "*" && lsearch == "*") {
for (int i = 0; i < p1.size(); i++) {
std::string firstName;
std::string lastName;
InfoType p1Value;
p1.get(i, firstName, lastName, p1Value);
result.add(firstName, lastName, p1Value);
}
}
else if (fsearch == "*" && lsearch != "*") {
for (int j = 0; j < p1.size(); j++) {
std::string firstName;
std::string lastName;
InfoType p1Value;
p1.get(j, firstName, lastName, p1Value);
if (lastName==lsearch) {
result.add(firstName, lastName, p1Value);
}
}
}
else if (fsearch != "*" && lsearch == "*") {
for (int j = 0; j < p1.size(); j++) {
std::string firstName;
std::string lastName;
InfoType p1Value;
p1.get(j, firstName, lastName, p1Value);
if (firstName == fsearch) {
result.add(firstName, lastName, p1Value);
}
}
}
else {
if (p1.contains(fsearch,lsearch)) {
InfoType p1Value;
p1.lookup(fsearch, lsearch, p1Value);
result.add(fsearch, lsearch, p1Value);
}
else{
return;
}
}
}
Explanation / Answer
void search(const std::string& fsearch, const std::string& lsearch, const PeopleList& p1, PeopleList& result) {
if (fsearch == "*" && lsearch == "*") {
for (int i = 0; i < p1.size(); i++) {
std::string firstName;
std::string lastName;
InfoType p1Value;
p1.get(i, firstName, lastName, p1Value);
result.add(firstName, lastName, p1Value);
}
}
else if (fsearch == "*" && lsearch != "*") {
for (int j = 0; j < p1.size(); j++) {
std::string firstName;
std::string lastName;
InfoType p1Value;
p1.get(j, firstName, lastName, p1Value);
if (lastName==lsearch) {
result.add(firstName, lastName, p1Value);
}
}
}
else if (fsearch != "*" && lsearch == "*") {
for (int j = 0; j < p1.size(); j++) {
std::string firstName;
std::string lastName;
InfoType p1Value;
p1.get(j, firstName, lastName, p1Value);
if (firstName == fsearch) {
result.add(firstName, lastName, p1Value);
}
}
}
else {
if (p1.contains(fsearch,lsearch)) {
InfoType p1Value;
p1.lookup(fsearch, lsearch, p1Value);
result.add(fsearch, lsearch, p1Value);
}
else{
return;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.