在 stl 列表中查找名称 C++

finding name in the stl list c++

我真的很难做这个练习 问题来了

修改程序 13.2 以提示用户输入姓名让程序在现有列表中搜索输入的姓名。如果名称在列表中,则显示相应的 phone 编号;否则,显示此消息:该名称不在 phone 列表中。

这里是程序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,所以我们将不胜感激。

您可以使用 header <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 found\n";

当然你应该像这样声明函数 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 found\n";

请注意,要输入名称,您应该使用标准函数 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