首页 > 解决方案 > 程序不会返回匹配搜索的名称计数

问题描述

嗨,我在这个 C++ 实验室需要一些帮助。 实验室说明我需要获取用户输入的名称,然后搜索与该输入匹配的名称。我似乎无法在我的代码中找出问题所在。我觉得它介于这两条线之间

 Person searchPerson(searchLastName, searchFirstName, 0);
 Person* sPerson = &searchPerson;
 // get count of such persons
 int cntPersons = persSet.count(sPerson);
 cout << "Number of people with this name= " << cntPersons;

我需要输出与用户输入的名称匹配的名称,但它一直返回 0。我需要对此有一些新的看法,因为我觉得这是一个我看不到的简单修复。完整代码贴在下面

#pragma warning (disable:4786)  // for set (Microsoft only)
#include <iostream>
#include <set>
#include <iterator>
#include <string>
#include <functional>
using namespace std;

class Person
{
private:
     string lastN, firstN;
     long phoneNum;
public:
     Person()
     {
          lastN = "blank";
          firstN = "blank";
          phoneNum = 0;
     }

     Person(string lastName, string firstName, long number)
     {
          lastN = lastName;
          firstN = firstName;
          phoneNum = number;
     }
     //operator == for person class 
     friend bool operator==(const Person& p1, const Person& p2)
     {
          return (p1.lastN == p2.lastN &&
               p1.firstN == p2.firstN) ? true : false;
     }

     friend bool operator<(const Person& p1, const Person& p2)
     {
          if (p1.lastN == p2.lastN)
               return (p1.firstN < p2.firstN) ? true : false;
          return (p1.lastN < p2.lastN) ? true : false;
     }

     void display() const //display person's data
     {
          cout << endl << lastN << ",\t" << firstN;
          cout << "\t\tPhone: " << phoneNum;
     }

};

int main()
{
     string searchLastName, searchFirstName;
     Person* ptrP1 = new Person("KuangThu", "Bruce", 4157300);
     Person* ptrP2 = new Person("Deauville", "William", 8435150);
     Person* ptrP3 = new Person("Wellington", "John", 9207404);
     Person* ptrP4 = new Person("Bartoski", "Peter", 6946473);
     Person* ptrP5 = new Person("Fredericks", "Roger", 7049982);
     Person* ptrP6 = new Person("McDonald", "Stacey", 7764987);
     Person* ptrP7 = new Person("KuangThu", "Bruce", 4157300);
     Person* ptrP8 = new Person("Deauville", "William", 8435150);
     // multiset of persons
     multiset<Person*, less<Person*> > persSet;
     // iterator to a multiset of persons
     multiset<Person*, less<Person*> >::iterator iter;
     persSet.insert(ptrP1);    // put persons in multiset
     persSet.insert(ptrP2);
     persSet.insert(ptrP3);
     persSet.insert(ptrP4);
     persSet.insert(ptrP5);
     persSet.insert(ptrP6);
     persSet.insert(ptrP7);
     persSet.insert(ptrP8);
     cout << "Number of Entries= " << persSet.size();
     iter=persSet.begin(); //display contents of multiset
     while (iter != persSet.end())
     {
               (*iter++)->display();
     }
     cout << endl<<"Enter the last name of person to search for: ";
     cin >> searchLastName;
     cout << endl<<"Enter their first name: ";
     cin >> searchFirstName;
     //creating a search for a person with this name 
     Person searchPerson(searchLastName, searchFirstName, 0);
     Person* sPerson = &searchPerson;
     // get count of such persons
     int cntPersons = persSet.count(sPerson);
     cout << "Number of people with this name= " << cntPersons;
     //output all the matches to the name
     iter = persSet.lower_bound(sPerson);
     while (iter != persSet.upper_bound(sPerson))
     {
          (*iter++)->display();
     }
     cout << endl;
     return 0;
}

标签: c++pointersvisual-c++

解决方案


推荐阅读