Anas Zahed Ответов: 1

Unordered_map вставить класс C++


Эй, все!!!
что я здесь делаю не так,
Тринг, чтобы передавать значение, чтобы вставить в метод класса ?

Implement a class course to manage the participants of a
Course.
Realize a constructor with which the title of the course can be determined;

and methods for entering names and student numbers for the individual female/male 
students,
which are identified by a number assigned to you (i.e., an index).
It must be possible (through appropriate methods)

спасибо!!!

Что я уже пробовал:

<pre>#include<iostream>
#include<string>
#include<unordered_map>
 
using namespace std;

class Student {
private:
	std::string name;
	int mrt_nr;

public:
	void set_name(string);
	void set_nr(int);
	string get_name();
	int get_mrt_nr();

};
void Student::set_name(string a) {
	name = a;

}
void Student::set_nr(int a) {
	mrt_nr = a;
}

string Student::get_name() {


	return name;
}

int Student::get_mrt_nr() {

	return mrt_nr;


}
class course {

private:
	string titel_lehrv;
	unordered_map<int, std::string> st;
public:
	course(const std::string &titel) : titel_lehrv{ titel } {};

	void add( Student &a ) {

		st.insert(pair<int, string>(a.get_mrt_nr, a.get_name));
	}

};


	int main() {
	
		pair<int, std::string>s1(12545, "tom");
		Student st;
		st.set_name ("tom") ;
		st.set_nr (123);
		course mycourse();
		mycourse().add(st);
		


	}

		    
                    

Xynosural.net

Больше информации ? что вы хотите сделать, что идет не так ?

Anas Zahed

я получаю много ошибок !!!
пытался оторваться от них, но ничего не делал.

Patrice T

а сообщения об ошибках есть ?

Anas Zahed

если я сделаю это с вектором я не получу никакой ошибки но я хочу сделать это с картой

1 Ответов

Рейтинг:
2

CPallini

Исправлено для вас:
Пожалуйста, обратите внимание: C++, unilike C# не имеет синтаксического сахара для свойств объекта, вы должны использовать стандартный синтаксис для вызова метода.

#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;

class Student
{
private:
  std::string name;
  int mrt_nr;

public:
  void set_name(string);
  void set_nr(int);
  string get_name();
  int get_nr();
};

void Student::set_name(string a)
{
  name = a;
}
void Student::set_nr(int a)
{
  mrt_nr = a;
}

string Student::get_name() {
  return name;
}

int Student::get_nr() {
  return mrt_nr;
}

class Course
{

private:
  string titel_lehrv;
  unordered_map<int, std::string> st;
public:
  Course(const std::string &titel) : titel_lehrv{ titel } {};

  void add( Student &a ) {
    st.insert(pair<int, string>(a.get_nr(), a.get_name()));
  }
};

int main()
{
  pair<int, std::string>s1(12545, "tom");
  Student st;
  st.set_name("tom");
  st.set_nr(123);
  Course mycourse("C++ programming");
  mycourse.add(st);
}




Ваш код мог бы быть более лаконичным:
#include <iostream>
#include <unordered_map>

using namespace std;

class Student
{
private:
  string  name;
  int no;
public:
  Student(string name, int no):name(name), no(no){}
  void set_name(const string & new_name){name = new_name;}
  string get_name() const { return name;}
  void set_no(int  new_no){no = new_no;}
  int get_no() const { return no;}
};

class Course
{
private:
  string title;
  unordered_map < int, string > student_map;
public:
  Course( const string title ) : title(title){}
  void add( const Student & student)
  {
    student_map[student.get_no()] = student.get_name();
  }
  void print() const
  {
    cout << title << "\n";
    for (const auto & [ no, name ] : student_map)
      cout << no << ", " << name << "\n";
  }
};

int main()
{
  Course course("C++ programming");
  course.add( Student("john", 10));
  course.add( Student("jim", 100001));
  course.add( Student("tom", 20210));
  course.print();
}