Unordered_map插入类C ++ [英] Unordered_map insert class C++
本文介绍了Unordered_map插入类C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里做错了什么,
将值传递给类方法中的插入?
实施课程以管理
课程的参与者。
实现一个构造函数,用它来确定课程的标题;
以及为个别女性/男性
学生输入姓名和学号的方法,
,由分配给您的号码(即索引)标识。
必须有可能(通过适当的方法)
谢谢!!!
我尝试过:
< pre> #include< iostream>
#include< string>
#include< unordered_map>使用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(){
返回名称;
}
int Student :: get_mrt_nr(){
return mrt_nr;
}
课程{
private:
string titel_lehrv;
unordered_map< int,std :: string> ST;
public:
course(const std :: string& titel):titel_lehrv {titel} {};
void add(学生& a){
st.insert(pair< int,string>(a.get_mrt_nr,a.get_name));
}
};
int main(){
pair< int,std :: string> s1(12545,tom);
学生;
st.set_name(tom);
st.set_nr(123);
course mycourse();
mycourse()。add(st);
}
解决方案
为您修好:
请注意:C ++
,unilikeC#
没有对象属性的语法糖,你必须使用标准语法进行方法调用。
#include < iostream >
#include < string >
#include < unordered_map >
使用 命名空间标准;
class 学生
{
私人:
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(字符串a)
{
name = a;
}
void Student :: set_nr( int a)
{
mrt_nr = a;
}
字符串Student :: get_name(){
return name;
}
int Student :: get_nr(){
return mrt_nr;
}
class 课程
{
私人:
string titel_lehrv;
unordered_map< int,std :: string> ST;
public :
课程( const std :: string& titel): titel_lehrv {titel} {};
void add(学生& a){
st.insert(pair< int,string>(a.get_nr( ),a.get_name()));
}
};
int main()
{
pair< int,std :: string> s1( 12545 , tom);
学生;
st.set_name( tom);
st.set_nr( 123 );
课程mycourse( C ++编程);
mycourse.add(st);
}
您的代码可能更简洁:
#include < iostream >
#include < unordered_map >
使用 namespace std;
class 学生
{
私人:
字符串名称;
int no;
public :
Student(字符串名称, 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 课程
{
private :
字符串标题;
unordered_map< int ,字符串> student_map;
public :
课程( const 字符串标题):title(title){ }
void add( const 学生和学生)
{
student_map [student.get_no()] = student.get_name();
}
void print() const
{
cout<<标题<< \ n;
for ( const auto & [no,name]:student_map)
cout<<没有<< ,<<名称<< \ n;
}
};
int main()
{
课程( C ++编程);
course.add(学生( john, 10 跨度>));
course.add(学生( jim, 100001 跨度>));
course.add(学生( tom, 20210 跨度>));
course.print();
}
heeey everyone!!!
what i'm doning wrong here,
tring to pass a value to an insert in class method ?
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)
thank!!!
What I have tried:
<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); }
解决方案
Fixed for you:
Please note:C++
, unilikeC#
has NO syntactic sugar for object properties, you have to use standard syntax for method invocation.
#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); }
Your code could be more terse:
#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(); }
这篇关于Unordered_map插入类C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文