gpt4 book ai didi

C++,copy_if 与 vector

转载 作者:行者123 更新时间:2023-12-03 12:47:18 25 4
gpt4 key购买 nike

这是代码的一部分,我遇到了麻烦

#include<iostream>
#include<fstream>
#include<conio.h>
#include<list>
#include<iterator>
#include<map>
#include<algorithm>
#include<ctime>
#include<sstream>
#include<vector>
#include<numeric>
#include<string>

using namespace std;

class C_Person
{
protected:
string M_SName;
string M_SEGN;

public:
inline C_Person& operator=(const C_Person& c_p) {
this->M_SName = c_p.M_SName;
this->M_SEGN = c_p.M_SEGN;
return *this;
}
};

class C_Student :public C_Person
{
public:

C_Student() {};

C_Student(const string Name, const string EGN, const string FN, const map<string, unsigned> st_book)
{
this->SetName(Name);
this->SetEGN(EGN);
this->SetFN(FN);
this->Student_Book = st_book;
}

C_Student(const C_Student &stud)
{
this->SetName(stud.GetName());
this->SetEGN(stud.GetEGN());
this->M_SFacNum = stud.M_SFacNum;
this->Student_Book = stud.Student_Book;
}

C_Student operator=(const C_Student&st) {
this->M_SName = st.M_SName;
this->M_SEGN = st.M_SEGN;
this->M_SFacNum = st.M_SFacNum;
}
private:
string M_SFacNum;
map <string, unsigned> Student_Book;
};

class C_Speciality
{
public:
C_Speciality(string spec, vector<C_Student>Vec_St) :
M_SSpec(spec),
V_Students(Vec_St)
{}

C_Speciality(const string &filename)//3.1*
{
try {
ifstream file(filename);
if (!file)
{
exception e;
throw e;
}
file >> this->M_SSpec;
copy(istream_iterator<C_Student>(file), istream_iterator<C_Student>(), back_inserter(this->V_Students));
file.close();
}
catch (exception e)
{
cout << "File did not open!";
}
}

list<C_Student*> GetStudentsWithDegeree(string const disciplineName, unsigned const degree) {//3.3*
list<C_Student*> listt;

copy_if(V_Students.begin(), V_Students.end(), back_inserter(listt),
[=](C_Student&st) {return st.GetDegreeByDisciplineName(disciplineName) == degree; });
return listt;
}
private:
string M_SSpec;
vector<C_Student> V_Students;
};

然后

copy_if(V_Students.begin(), V_Students.end(), back_inserter(listt),
[=](C_Student&st) {return st.GetDegreeByDisciplineName(disciplineName) == degree; }
);

它给了我

    Error   C2679   binary '=': no operator found which takes a right-hand operand of type 'C_Student' (or there is no acceptable conversion)   C++_echo    c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\algorithm   585 

或者完整输出

c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\algorithm(585): error C2679: binary '=': no operator found which takes a right-hand operand of type 'C_Student' (or there is no acceptable conversion)
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xutility(4233): note: could be 'std::back_insert_iterator<std::list<C_Student *,std::allocator<_Ty>>> &std::back_insert_iterator<std::list<_Ty,std::allocator<_Ty>>>::operator =(std::back_insert_iterator<std::list<_Ty,std::allocator<_Ty>>> &&)'
1> with
1> [
1> _Ty=C_Student *
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xutility(4233): note: or 'std::back_insert_iterator<std::list<C_Student *,std::allocator<_Ty>>> &std::back_insert_iterator<std::list<_Ty,std::allocator<_Ty>>>::operator =(const std::back_insert_iterator<std::list<_Ty,std::allocator<_Ty>>> &)'
1> with
1> [
1> _Ty=C_Student *
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xutility(4210): note: or 'std::back_insert_iterator<std::list<C_Student *,std::allocator<_Ty>>> &std::back_insert_iterator<std::list<_Ty,std::allocator<_Ty>>>::operator =(C_Student *&&)'
1> with
1> [
1> _Ty=C_Student *
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xutility(4204): note: or 'std::back_insert_iterator<std::list<C_Student *,std::allocator<_Ty>>> &std::back_insert_iterator<std::list<_Ty,std::allocator<_Ty>>>::operator =(C_Student *const &)'
1> with
1> [
1> _Ty=C_Student *
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\algorithm(585): note: while trying to match the argument list '(std::back_insert_iterator<std::list<C_Student *,std::allocator<_Ty>>>, C_Student)'
1> with
1> [
1> _Ty=C_Student *
1> ]
1>c:\users\echo\source\repos\c++_lubo_kursova\c++_lubo_kursova\source.cpp(304): note: see reference to function template instantiation '_OutIt std::copy_if<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>,std::back_insert_iterator<std::list<C_Student *,std::allocator<C_Student*>>>,C_Speciality::GetStudentsWithDegeree::<lambda_3c3836fbaa0c70067d1b37845af861f2>>(_InIt,_InIt,_OutIt,_Pr)' being compiled
1> with
1> [
1> _OutIt=std::back_insert_iterator<std::list<C_Student *,std::allocator<C_Student *>>>,
1> _Ty=C_Student,
1> _InIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<C_Student>>>,
1> _Pr=C_Speciality::GetStudentsWithDegeree::<lambda_3c3836fbaa0c70067d1b37845af861f2>
1> ]
1>Done building project "C++_echo.vcxproj" -- FAILED.

我可以看到没有使用C_Student的operator=,但是我做错了什么?仅仅是运算符重载吗?我还想问一下 lambda 在这里使用是否正确。我应该用比较器替换它吗(我只使用过一次)?

最佳答案

在您的 GetStudentsWithDegeree 中,您尝试从 V_Students vector 中获取对象,并将其放入 C_Student* 的 list 即指向对象的指针。这是正确的成员函数代码:

    list<C_Student> GetStudentsWithDegeree(string const disciplineName, unsigned const degree) {
list<C_Student> listt;

copy_if(V_Students.begin(), V_Students.end(), back_inserter(listt),
[=] (const C_Student &st) {
return st.GetDegreeByDisciplineName(disciplineName) == degree;
});
return listt;
}

关于 lambda 的使用,这只是使用函数对象的现代方式。您可以自由使用任何适合您的方法。在这种情况下,std::lessstd::greater 等比较器不适合,因为您使用自定义代码选择必要的对象,而不是对它们进行排序。

关于C++,copy_if 与 vector <class>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55552746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com