无法解释此 C++ 代码
Having trouble interpreting this C++ code
有关代码发布的背景: PayRoll 是 class 的名称。 personSalary是double型变量,personAge是integer型变量。给出的代码是按年龄或薪水对列表进行排序。
struct less_than_salary
{
inline bool operator() (const PayRoll& struct1, const PayRoll& struct2)
{
return (struct1.personSalary < struct2.personSalary);
}
};
struct less_than_age
{
inline bool operator() (const PayRoll& struct1, const PayRoll& struct2)
{
return (struct1.personAge < struct2.personAge);
}
};
我想要一些帮助来理解给定代码的这一部分。我已经尝试阅读 struct 的用途,据我了解,它基本上作为 class 运行,并允许您一次处理多种类型的变量。如果我错了,那么在这种情况下结构到底有什么用?
另外,如果有人解释一下 "inline bool operator()" 在做什么,我将不胜感激,因为我以前从未见过,而且我无法通过阅读教科书来理解。
感谢您的帮助!
这两个结构都是所谓的 "functor" 的实现。给定用法
PayRoll x;
PayRoll y
// initialise x and y
less_than_salary f;
if (f(x,y)) // this calls less_than_salary::operator()(x,y)
std::cout << "X has lower salary than Y\n";
else
std::cout << "X has higher salary than Y\n";
此外,给定一个 PayRoll
的数组,可以排序
PayRoll a[20];
// initialise elements of a
less_than_salary f;
std::sort(a, a+20, f); // will sort in order of ascending salary
std::sort(a, a+20, less_than_salary()); // does same as preceding two lines
也可以使用标准容器(std::vector<PayRoll>
等)。
less_than_age
允许做本质上相同的事情,但使用年龄而不是薪水作为排序标准。
这里没有函数重载。两种结构类型都提供 operator()
,但这不是重载。
像这样的结构可以用在STL函数sort
中,它在std::list
等数据结构中是可用的。
#include <list>
#include <iostream>
int main() {
std::list<int> example = {9,8,7,6,5,4,3,2,1};
struct {
bool operator()( const int lhs, const int rhs) {
return lhs < rhs;
}
} compare_list;
example.sort(compare_list);
for(auto &i : example) {
std::cout<<i<<" ";
}
}
输出:
1 2 3 4 5 6 7 8 9
要了解其工作原理,请考虑以下内容:
//..
testS(compare_list);
//..
template <typename T>
void testS(T t) {
std::cout<< t(4,5) << "\n";
std::cout<< t(5,4) << "\n";
}
输出将是
1
0
有关代码发布的背景: PayRoll 是 class 的名称。 personSalary是double型变量,personAge是integer型变量。给出的代码是按年龄或薪水对列表进行排序。
struct less_than_salary
{
inline bool operator() (const PayRoll& struct1, const PayRoll& struct2)
{
return (struct1.personSalary < struct2.personSalary);
}
};
struct less_than_age
{
inline bool operator() (const PayRoll& struct1, const PayRoll& struct2)
{
return (struct1.personAge < struct2.personAge);
}
};
我想要一些帮助来理解给定代码的这一部分。我已经尝试阅读 struct 的用途,据我了解,它基本上作为 class 运行,并允许您一次处理多种类型的变量。如果我错了,那么在这种情况下结构到底有什么用? 另外,如果有人解释一下 "inline bool operator()" 在做什么,我将不胜感激,因为我以前从未见过,而且我无法通过阅读教科书来理解。 感谢您的帮助!
这两个结构都是所谓的 "functor" 的实现。给定用法
PayRoll x;
PayRoll y
// initialise x and y
less_than_salary f;
if (f(x,y)) // this calls less_than_salary::operator()(x,y)
std::cout << "X has lower salary than Y\n";
else
std::cout << "X has higher salary than Y\n";
此外,给定一个 PayRoll
的数组,可以排序
PayRoll a[20];
// initialise elements of a
less_than_salary f;
std::sort(a, a+20, f); // will sort in order of ascending salary
std::sort(a, a+20, less_than_salary()); // does same as preceding two lines
也可以使用标准容器(std::vector<PayRoll>
等)。
less_than_age
允许做本质上相同的事情,但使用年龄而不是薪水作为排序标准。
这里没有函数重载。两种结构类型都提供 operator()
,但这不是重载。
像这样的结构可以用在STL函数sort
中,它在std::list
等数据结构中是可用的。
#include <list>
#include <iostream>
int main() {
std::list<int> example = {9,8,7,6,5,4,3,2,1};
struct {
bool operator()( const int lhs, const int rhs) {
return lhs < rhs;
}
} compare_list;
example.sort(compare_list);
for(auto &i : example) {
std::cout<<i<<" ";
}
}
输出:
1 2 3 4 5 6 7 8 9
要了解其工作原理,请考虑以下内容:
//..
testS(compare_list);
//..
template <typename T>
void testS(T t) {
std::cout<< t(4,5) << "\n";
std::cout<< t(5,4) << "\n";
}
输出将是
1
0