【问题标题】:searching for name using vectors使用向量搜索名称
【发布时间】:2013-11-25 08:37:11
【问题描述】:

如何从程序中搜索用户输入的名称?目前我有这个用于我的搜索,但是每当程序找到选定的学生时,它就会像一个永无止境的循环一样不停地打印。我也为此使用了多态性,因为学生分为本地学生和国际学生。

我曾想过使用 stl 算法来遍历学生,但我对 stl 很陌生。我尝试了一些来自 Internet 的示例,但它在应用于我的程序时总是给我一个错误。

主要功能

int main()
{
    clsUniversityProgram objProgram[3];

    for (int x = 0; x < 3; x++)
    {
        objProgram[x].programInfo();
    }

    vector <clsStudent*> student;
    addStudents(student, objProgram);
    searchStudent(student);
    return 0;
}


void searchStudent(const vector <clsStudent*>& s)
{
    string searchName;
    const clsStudent *foundStudent;

    cout << "\nEnter student name to search for. [ENTER] terminates" << endl;
    cin >> searchName;

    if (s.size() == 0)
        cout << "There is 0 student in the database.";

    while(searchName.length() != 0)
    {
        for (int i = 0; i < s.size(); i++)
        {
            if (s[i]->getName() == searchName)
            {
                cout << "Found " << searchName << "!";
               // s[i]->print();
                break;
            }
            else
            {
                cout << "No records for student: " << searchName;
                cout << "\nEnter student name to search for. [ENTER] terminates" << endl;
                cin >> searchName;
            }

        }
    }
}

【问题讨论】:

标签: c++ vector polymorphism stl-algorithm


【解决方案1】:

如何从程序中搜索用户输入的名称?

使用std::find_if

auto it = std::find_if(s.begin(), 
                       s.end(),
                       [&searchName](const clsStudent* student)
                       { return student->getName() == searchName; });
if (it != s.end()) {
  // name was found, access element via *it or it->
else {
  // name not found
}

C++03版本:

struct MatchName
{
  MatchName(const std::string& searchName) : s_(searchName) {}
  bool operator()(const clsStudent* student) const
  {
    return student->getName() == s_;
  }
 private:
  std::string s_;
};


vector<clsStudent*>::iterator it = std::find_if(s.begin(), 
                                                s.end(),
                                                MatchName(searchName));

// as before

【讨论】:

  • [&searchName](const clsStudent* student) 这行代码有什么作用?
  • @AlexiusLim 它是一个lambda function,它通过引用捕获searchName,并将clsStudent* 作为参数。
  • 自动在这里似乎不起作用。我的编译器给了我一个错误,说它没有命名类型
  • @AlexiusLim 也许你的编译器不支持 C++11。我发布的所有内容都有 C++03 替代品。
  • @AlexiusLim 我添加了一个 C++03 版本。
【解决方案2】:

因为如果找到学生,则打印姓名,然后打破for-循环,当while 条件被重新评估并且仍然为真时,因为searchName 没有改变。您应该将 searchName 设置为长度为 0 的字符串,或者使用其他一些条件来突破 while

【讨论】:

    【解决方案3】:

    应用程序不断打印结果,因为您的条件while(searchName.length() != 0) 仍然有效。当您编写 break 时,您会跳出 for 循环 for (int i = 0; i &lt; s.size(); i++)。例如:

    while (true) {
        for (;;) {
            std::cout << "I get printed forever, because while keeps calling this for loop!" << std::endl;
            break;
        }
    
        // Calling break inside the for loop arrives here
        std::cout << "I get printed forever, because while's condition is always true!" << std::endl;
    }
    

    如果您试图让用户不断搜索学生的集合(即类似于“找到/未找到的学生,找到另一个?”的内容),您需要包含这些行...

    cout << "\nEnter student name to search for. [ENTER] terminates" << endl;
    cin >> searchName;
    

    ... 在您的 while 循环中修改 searchName 并提示用户空输入将导致程序退出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-04
      • 2012-07-29
      • 2011-07-27
      • 2014-09-21
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      相关资源
      最近更新 更多