【问题标题】:How to search array of structs for a string(name) and return info for that string(name)?如何在结构数组中搜索字符串(名称)并返回该字符串(名称)的信息?
【发布时间】:2014-04-17 22:18:07
【问题描述】:

使用此程序,当我输入名称时,不会返回任何内容。

我该如何解决这个问题?

有 1000 行信息如下所示:

114680858 19670607 玛蒂尔达文森特 MI

114930037 19471024 Desdemona Hanover ID

115550206 19790110 Xanadu Perlman ND

116520629 19630921 亚历山大大厅 SD

117050976 19301016 大卫·兰普雷 GA

119610646 19650202 托马斯·波洛克 IL

120330928 19621126 加里卡特曼 NC

等等……

代码:

struct employees
{
    int ss_number;//social security
    int dob;//date of birth YYYY/MM/DD Ex.) 19870314=1987/03/14
    string f_name;
    string l_name;
    string state; //state of residence
};

void read_file()//read file into array of 1000 structs
{
    ifstream data("/home/www/class/een118/labs/database1.txt");
    employees array[1000]
    if(!data.fail())
    {
        int i;
        for(int i=0;i<1000;i++)
        {
            data>>array[i].ss_number
                >>array[i].dob
                >>array[i].f_name
                >>array[i].l_name
                >>array[i].state;
        }
        for(int i=0;i<1000;i++)
        {
            cout<<array[i].ss_number>>" "<<array[i].dob>>" "<<array[i].f_name>>" "<<
            array[i].l_name>>" "<<array[i].state;
        }
    }
}

void print_person(employees e)
{
    cout<<e.ss_number>>" "<<e.dob>>" "<<e.f_name>>" "<<e.l_name>>" "<<e.state;
}

void search(employees array[])//type in name and get that persons ss_number,dob etc...
{
    string first;
    string last;
    cout<<"Enter name";
    cin>>first>>last;
    for(int i=0;i<1000;i++)
    {
        if(array[i].f_name==first && array[i].l_name==last)
        {
            print_person(array[i]);
        }
    }
}

void main()
{
    employees array[10];
    read_file();
    search(array);
}
//  ...

【问题讨论】:

  • 使用调试器时变量的值是多少?
  • 请记住,== 对字符串进行区分大小写的比较。在比较之前,您可能需要转换为全部小写或全部大写。
  • 您的cout 行中充满了&gt;&gt; .. 我什至没有

标签: c++ arrays string struct


【解决方案1】:

在你的search 函数中,你不应该使用相等比较

if (array[i].f_name==first)

您应该使用std::string::compare

if (array[i].f_name.compare(first) == 0)

这将以与您期望 == 相同的方式返回 True。

【讨论】:

  • 使用std::string::operator==()有什么问题?我没有遇到任何问题。
  • 恕我直言,更好的解决方案是在结构中添加比较方法。
【解决方案2】:

有两个数组。一个在main,另一个在read_file。它们具有相同的名称,但大小不同。

read_file 中的数组与main 中的数组没有关系。您将数组传递给search,但没有传递给read_file。我建议您通过引用将数组传递给read_file,并删除read_file 中的数组声明。

更好的是,消除数组并使用std::vector。应该是std::vector&lt;employees&gt;

编辑 1:搜索数组
search 函数中,您需要传递两个附加参数:数组容量和数组中的记录数。如果您使用std::vector&lt;employees&gt;,您可以通过以下方式获取数组中的员工人数:

  number_of_employees = array.size();

for 循环将使用迭代器:

std::vector<employees>::const_iterator iter;
for (iter = array.begin(); iter != array.end(); ++iter)
{
  // process array slot by dereferencing it:
    employee e = *iter;
    cout << e << "\n"; // This could happen if you overloaded operator <<
}

否则,使用数组,您的循环将如下所示:

void search(employees array[], unsigned int capacity, unsigned int employees_in_array)
{
  for (unsigned int i = 0; i < employees_in_array; ++i)
  {
    cout << array[i];
  }
}

一个很好的改进是这个搜索功能不会硬编码大小。因此,您可以将大小从 10(main)更改为 1000,而无需修改 search 函数。

如果您对容器进行排序,则可以使用二分搜索。
见:std::binary_search, std::find, std::lower_bound, std::upper_bound

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2014-10-26
    • 1970-01-01
    • 2015-12-16
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多