在stl列表c++中查找name

finding name in the stl list c++

本文关键字:查找 name c++ stl 列表      更新时间:2023-10-16

我在这个练习上真的很吃力有问题

修改程序13.2以提示用户输入名称,让程序在现有列表中搜索输入的名称。如果姓名在列表中,则显示相应的电话号码;否则,显示以下信息:姓名不在电话列表中。

这是程序13.2 的代码

#include <iostream>
#include <list>
#include <string>
using namespace std;
class Nametele
{
private:
    string name;
    string phoneNum;
public:
    Nametele(string nn, string phone)
    {
        name = nn;
        phoneNum = phone;
    }
    string getName(){return name;}
    string getPhone(){return phoneNum;}
};

int main()
{
    list<Nametele> employee;
    string n;
    employee.push_front(Nametele("acme, sam", "(555) 898-2392"));
    employee.push_back(Nametele("Dolan, edith", "(555) 682-3104"));
    employee.push_back(Nametele("lanfrank, john", "(555), 718-4581"));
    employee.push_back(Nametele("mening, stephen", "(555) 382-7070"));
    employee.push_back(Nametele("zemann, harold", "(555) 219-9912"));
    employee.sort();
    cout << "the size of the list is " << employee.size() << endl;
    cout << "n      name            telephone";
    cout << "n--------------       ----------n";
    while(!employee.empty())
    {
        cout << employee.front().getName()
             << "t      " << employee.front().getPhone() << endl;
        employee.pop_front();
    }
    return 0;
}

我真的不知道如何找到列表中的元素。我对编程真的很陌生,尤其是STL,所以任何帮助都将不胜感激。

您可以使用标头<algorithm> 中声明的标准算法std::find_if

例如

#include <algorithm>
//...
std::string  name = "mening, stephen";
auto it = std::find_if( employee.begin(), employee.end(),
                        [&]( Nametele &item ) { return item.getName() == name; } );
if ( it != employee.end() ) std::cout << "Employee " << name << " is foundn";

当然,您应该像一样声明函数getName

string getName() const {return name;}

并且在lambda表达式中声明类似const Nametele &item 的参数

如果列表已排序,则可以使用其他算法,例如std::lower_boundstd::equal_range或者可以使用循环。例如

std::string  name = "mening, stephen";
auto it = employee.begin();
while ( it != employee.end() && it->getName() != name ) ++it;
if ( it != employee.end() ) std::cout << "Employee " << name << " is foundn";

考虑到要输入名称,应该使用标准函数std::getline。例如

std::getline( std::cin, name );

我似乎已经明白你到底需要什么了。这是一个演示程序。:)

#include <iostream>
#include <iomanip>
#include <list>
#include <string>
#include <algorithm>
class Nametele
{
private:
    std::string name;
    std::string phoneNum;
public:
    Nametele( const std::string &nn, const std::string &phone )
        : name( nn ), phoneNum( phone )
    {
    }
    std::string getName() const { return name; }
    std::string getPhone() const { return phoneNum; }
};
bool operator <( const Nametele &lhs, const Nametele &rhs )
{
    return lhs.getName() < rhs.getName();
}
int main() 
{
    std::list<Nametele> employee;
    employee.push_back(Nametele("acme, sam", "(555) 898-2392"));
    employee.push_back(Nametele("Dolan, edith", "(555) 682-3104"));
    employee.push_back(Nametele("lanfrank, john", "(555), 718-4581"));
    employee.push_back(Nametele("mening, stephen", "(555) 382-7070"));
    employee.push_back(Nametele("zemann, harold", "(555) 219-9912"));
    employee.sort();
    std::cout << "the size of the list is " << employee.size() << std::endl;
    std::cout << "n      name            telephone";
    std::cout << "n--------------        ----------n";
    for ( const Nametele &item : employee )
    {
        std::cout << std::setw( 14 ) << std::left << item.getName()
                  << "t      " << item.getPhone() << std::endl;
    }
    std::cout << "Enter a name to find: ";
    std::string name;
    std::getline( std::cin, name );
    auto it = employee.begin();
    while ( it != employee.end() && it->getName() != name ) ++it;
    if ( it != employee.end() )
    {
        std::cout << "Employee " << it->getName()
                  << " has phone " << it->getPhone()
                  << std::endl;
    }
    return 0;
}

输出为

the size of the list is 5
      name            telephone
--------------        ----------
Dolan, edith          (555) 682-3104
acme, sam             (555) 898-2392
lanfrank, john        (555), 718-4581
mening, stephen       (555) 382-7070
zemann, harold        (555) 219-9912
Enter a name to find: lanfrank, john
Employee lanfrank, john has phone (555), 718-4581