谁能帮我实现在 C++ 中使用 stl 列表的程序

问题描述

我正在尝试学习 C++,现在我必须面对一个程序的实现,该程序要求我创建一个极客列表和一个要工作的项目列表。对于每个极客,我必须存储他的 ID 号和他的工资,对于每个项目,我必须存储它的名称和他的 ID 号。 然后它要求我将特定的极客与特定的项目相关联。

所以我的想法是创建一个带有 geek 类信息和 set/get 函数的 geek.h,然后创建一个带有项目类信息和 set/get 函数的 project.h,然后我想实现一个 addinformation将函数转换为 main.cpp 和另一个将特定极客链接到项目的函数。 现在我很挣扎,这是我的部分代码

#include<iostream>
#include<vector>
#include<list>
#include<string>
//geek.h
using std::cout;
using std::cin;
using std::vector;
using std::list;
using std::string;
class geek
{
  friend class project;
  private:
  int id_geek;
  double hour_salary;
  public:
      geek(int i,double  h) : id_geek{i},hour_salary{h} {}
      void setid(int id)
  {
      id_geek=id;
  }
  void seths(double hs)
  {
      hour_salary=hs;
  }
  int getid()
  {
      return id_geek;
  }
  double geths()
  {
     return hour_salary;
  }
};

#include<iostream>
#include<vector>
#include<list>
#include<string>
#include<iterator>
#include"geek.h"
//project.h
using std::iterator;
using std::vector;
using std::list;
using std::string;

class project: public geek
{
    friend class geek;
    private:
    int id_project;
    string project_name;
    double total_amount;
    double delivery_date;
    list<project> projectlist;
    public:
    project(int id,string pn,double ta,double dd,int idg,double hr) :id_project{id},project_name{pn},total_amount{ta},delivery_date{dd},geek(idg,hr) {}
    void setid(int id)
    {
        id_project=id;
    }
    void setpn(string pn)
    {
       project_name=pn;
    }
    void setta(double ta)
    {
        total_amount=ta;
    }
    void setdd(double dd)
    {
        delivery_date=dd;
    }
    int getid()
    {
        return id_project;
    }
    string getpn()
    {
        return project_name;
    }
    double getta()
    {
        return total_amount;
    }
    double getdd()
    {
        return delivery_date;
    }
};


#include<iostream>
#include<vector>
#include<list>
#include<string>
#include"project.h"
#include"geek.h"
//fantaware.cpp
using std::vector;
using std::list;
using std::string;

int main()
{
list<geek> geeklist;
list<project> projectlist;
int id ;
double salary;
  void Addgeekinfo(geek g)  
  {
     cout <<"insert geek id:";
     cin >> id;
     cout<<"insert geek salary;";
     cin>>salary;

     (Now how do i use geeklist.push-front(?) in order to put elements into the list??)
     (ll have the same prob with aggproject info that's why i did nit read it yet)

     (how can i create a function to link a geek to a project) 
  }
}

`

`

解决方法

您的代码中有一些奇怪的东西。 project 继承自 geek 并且是 geek 的朋友,这看起来很奇怪。那不是好的设计,可能你也不需要。 project 包含一个 list<project>,这也很奇怪。什么是project?它是单个项目还是项目列表?决定一个并相应地命名,它不能两者兼而有之。此外,您不能在函数内部定义函数。

另一方面,您的代码中也有一些看起来相当积极的东西:您使用了成员初始化列表,那是 +1!此外,您不使用 using namespace std; 而只有 using 声明您实际使用的东西。尽管将它们放在标题中可能会出现问题。不像 using namespace std; 那么多,但任何包含这些标头的代码也包含那些 using 声明。

你的代码有点笨拙,所以我允许自己减少很多。要将元素推送到列表中,您需要先创建一个元素然后推送它(push_frontpush_back),或者使用 emplace_front / emplace_back 来构造元素到位:

#include<iostream>
#include<list>

struct geek {
  int id_geek;
  double hour_salary;
  geek(int id_geek,double hour_salary) : id_geek(id_geek),hour_salary(hour_salary) {}
};

int main() {
    int id ;
    double salary;
    std::cout <<"insert geek id:";
    std::cin >> id;
    std::cout<<"insert geek salary;";
    std::cin>>salary;

    std::list<geek> geek_list;
    geek_list.emplace_back(id,salary);

    geek g{id,salary};
    geek_list.push_front(g);
}

您可能还想为 operator<<operator>> 提供重载,以便您可以编写更紧凑的代码:

geek g;
std::cin >> g;
geek_list.push_front(g);

有关详细信息,请参阅此答案:https://stackoverflow.com/a/4421719/4117728