C++,copy_if 与矢量<class>

C++, copy_if with vector<class>

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

#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 studio17\community\vc\tools\msvc.16.27023\include\algorithm   585 

或者完整输出

c:\program files (x86)\microsoft visual studio17\community\vc\tools\msvc.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 studio17\community\vc\tools\msvc.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 studio17\community\vc\tools\msvc.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 studio17\community\vc\tools\msvc.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 studio17\community\vc\tools\msvc.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 studio17\community\vc\tools\msvc.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 的向量中取出一个对象并放入 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 之类的比较器是不合适的,因为您 select 使用自定义代码的必要对象,而不是对它们进行排序。