在stl列表c++中查找name
finding name in the stl list c++
我在这个练习上真的很吃力有问题
修改程序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_bound
或std::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
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 在C++中查找文件
- 模板元程序查找相似的连续类型名称
- 在UNIX系统中使用DIR查找文件的字节大小
- 查找最接近的大于当前数字的数字的索引
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 查找后更改类变量
- 使用正则表达式regex_search在字符串中查找字符串
- 使用gcc从静态链接的文件中查找可选符号
- 在C++中查找范围的长度
- 算法问题:查找从堆栈中弹出的所有序列
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- 在Windows中查找扬声器输出的当前音量级别
- 如何在C++中使用X509证书模在令牌中查找私钥
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 在 for 循环中查找问题时遇到困难
- 如何在文件中查找字节序列
- 试图创建一个多线程程序来查找0-100000000之间的总素数
- 在stl列表c++中查找name