gpt4 book ai didi

c++ - 按整数对结构 vector 进行排序

转载 作者:行者123 更新时间:2023-11-30 05:40:38 24 4
gpt4 key购买 nike

<分区>

我正在尝试对结构 vector 进行排序。我看到了thisthis示例。

这是我的代码:

.h文件:

// I didn't mention all the includes and namespace
class fileLoader
{
struct commands
{
int time;
string name;
};
commands resultStruct;
vector<commands> resultVector

private:
void sortFunction();
bool compareByTime(const commands &a, const commands &b);
}

.cpp 文件:

void fileLoader::sortResults()
{
sort(resultVector.begin(), resultVector.end(), compareByTime);
}

bool fileLoader::compareByTime(const commands &a, const commands &b)
{
return a.time < b.time;
}

这是我得到的编译错误:

error C3867: 'fileLoader::compareByTime': function call missing argument list; use '&fileLoader::compareByTime' to create a pointer to member
error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided
c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3639) : see declaration of 'std::sort'

当我尝试将 compareByTime 更改为 &fileLoader::compareByTime 时,我遇到了这个编译错误:

    error C2064: term does not evaluate to a function taking 2 arguments
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3776) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Unguarded_partition<_RanIt,_Pr>(_RanIt,_RanIt,_Pr)' being compiled
1> with
1> [
1> _Ty1=fileLoader::commands *,
1> _Ty2=fileLoader::commands *,
1> _RanIt=fileLoader::commands *,
1> _Pr=bool (__thiscall fileLoader::* )(const fileLoader::commands &,const fileLoader::commands &)
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3806) : see reference to function template instantiation 'void std::_Sort<fileLoader::commands*,__w64 int,_Pr>(_RanIt,_RanIt,_Diff,_Pr)' being compiled
1> with
1> [
1> _Pr=bool (__thiscall fileLoader::* )(const fileLoader::commands &,const fileLoader::commands &),
1> _RanIt=fileLoader::commands *,
1> _Diff=__w64 int
1> ]
std::sort<std::_Vector_iterator<_Myvec>,bool(__thiscall fileLoader::* )(const fileLoader::commands &,const fileLoader::commands &)>(_RanIt,_RanIt,_Pr)' being compiled
1> with
1> [
1> _Myvec=std::_Vector_val<fileLoader::commands,std::allocator<fileLoader::commands>>,
1> _RanIt=std::_Vector_iterator<std::_Vector_val<fileLoader::commands,std::allocator<fileLoader::commands>>>,
1> _Pr=bool (__thiscall fileLoader::* )(const fileLoader::commands &,const fileLoader::commands &)
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3720): fatal error C1903: unable to recover from previous error(s); stopping compilation
1>
1>Build FAILED.

我很乐意提供一些帮助。

谢谢。

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