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

时间:2015-04-05 19:08:40

标签: c++ stl

我真的很难参加这项运动 这是问题

修改程序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,所以任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:2)

您可以使用标头std::find_if

中声明的标准算法<algorithm>

例如

#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 like

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