【问题标题】:How to search after a object name in c++如何在 C++ 中搜索对象名称
【发布时间】:2021-09-24 10:35:49
【问题描述】:

希望大家度过了美好的一天!

我的问题是我不知道如何搜索对象的名称(如果那是正确的放置方式)。但是一个例子是:我创建了一个包含名字、姓氏、年级和出生年份的学生班级。其中一名学生的名字可能是“艾萨克”。我想要做的是在“命令提示符”中写一个名字,结果应该是我写的学生姓名的信息。如果名字没有被列为学生之一,我应该可以再试一次。

下面是我的小代码。在“search_after_student_name”中应该是计算机搜索学生的名字以查看该学生信息的地方。

#include <iostream>
#include <Windows.h>
#include <cstring>
using namespace std;

class Student {
public:
    string first_name;
    string last_name;
    char grade;
    int birth;
    Student(string aFirst_name, string aLast_name, char aGrade, int aBirth) {
        first_name = aFirst_name;
        last_name = aLast_name;
        grade = aGrade;
        birth = aBirth;
    }
};

void search_after_student_name(string hisFirst_name)
{
    
    if (hisFirst_name != student.first_name) //Is not like that, but just an example of how I am going to use it
        cout << "We couldn't find the name you wrote. Try again" << endl;
    else() //if the "hisFirst_name" matches a student:
    {
        cout << "info about this student...";
    }
}

int main()
{
    SetConsoleCP(1252);
    SetConsoleOutputCP(1252);

    Student student1("Oliver", "Twist", 'C', 1999);
    Student student2("James", "Free", 'D', 2000);
    Student student3("Isaac", "Lee", 'A', 2000);

    string namn;
    cout << "Write in the name that you want to search: ";
    cin >> namn;

    search_after_student_name(namn);
    return 0;
} 

我希望你能理解我的愚蠢代码问题并帮助我。谢谢!

【问题讨论】:

  • 这段代码的编写方式你无法搜索任何内容。您的对象的范围是main,您的函数无法访问这些对象。如果您打算进行搜索,您应该将它们真正存储在某种数据结构中(例如:std::vectorstd::map
  • 您需要创建一个学生数组或向量,并将其与要搜索的名称一起传递给您的搜索函数。然后就可以使用this来查找对象了。
  • 我建议 OP 清除他/她的 OOP 概念。谁让数据成员公开?
  • @ILoveLogCat 这只是我想做的一个小任务,仅此而已。

标签: c++ class object


【解决方案1】:

正如 cmets 中所指出的,您应该使用一些数据结构来保存您的数据。这可能是一个解决方案:

#include <iostream>
#include <Windows.h>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class Student
{
public:
    string first_name;
    string last_name;
    char grade;
    int birth;
    Student(string aFirst_name, string aLast_name, char aGrade, int aBirth)
    {
        first_name = aFirst_name;
        last_name = aLast_name;
        grade = aGrade;
        birth = aBirth;
    }
};

void search_after_student_name(const string& hisFirst_name, const std::vector<Student>& students)
{
    auto studentIt = std::find_if(std::begin(students), std::end(students), [&hisFirst_name](const auto & s)
    {
        return s.first_name == hisFirst_name;
    });

    if (studentIt == std::end(students)) 
        cout << "We couldn't find the name you wrote. Try again" << endl;
    else //if the "hisFirst_name" matches a student:
    {
        cout << "info about this student: ";
        cout << studentIt->first_name << ", "
             << studentIt->last_name << ", "
             << studentIt->grade << ", "
             << studentIt->birth;
    }
}

int main()
{
    SetConsoleCP(1252);
    SetConsoleOutputCP(1252);

    std::vector<Student> students =
    {
        Student("Oliver", "Twist", 'C', 1999),
        Student("James", "Free", 'D', 2000),
        Student("Isaac", "Lee", 'A', 2000),
    };

    string namn;
    std::cout << "Write in the name that you want to search: ";
    std::cin >> namn;

    search_after_student_name(namn, students);
    return 0;
}

请注意,我将search_after_student_name 更改为通过const reference 获取数据,如果您不需要更改数据,这是better way 将数据传递给您的函数。

【讨论】:

    【解决方案2】:

    请注意,数据集合应存储在数组或其他类型的容器中。 另外为了保持代码干净,这里应该使用标准算法,为了练习 C++20,已经使用了std::ranges::find(我也清理了一些样板):

    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <ranges>
    
    class Student
    {
    public:
        std::string first_name;
        std::string last_name;
        char grade;
        int birth;
    };
    
    void printStudent(const Student& x)
    {
        std::cout << "first_name: " << x.first_name << '\n';
        std::cout << " last_name: " << x.last_name << '\n';
        std::cout << "     grade: " << x.grade << '\n';
        std::cout << "     birth: " << x.birth << '\n';
    }
    
    int main()
    {
        std::vector students {
            Student{"Oliver", "Twist", 'C', 1999},
            {"James", "Free", 'D', 2000},
            {"Isaac", "Lee", 'A', 2000},
        };
    
        std::string name;
        std::cout << "Write in the name that you want to search: ";
        std::cin >> name;
    
        auto it = std::ranges::find(students, name, &Student::first_name);
        if (it == students.end()) {
            std::cout << "\nNot found\n";
        } else {
            std::cout << "\nStudent found:\n";
            printStudent(*it);
        }
        return 0;
    }
    

    https://godbolt.org/z/GdafW9Yjv

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-21
      • 2021-12-02
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      相关资源
      最近更新 更多