[英]Is it possible to use std::sort with a sort function that takes extra arguments?
这是我一直在考虑的事情。 我已经做了一些研究而且找不到任何东西,但我也没有发现任何相反的东西。
考虑<algorithm>
的std::sort
函数。 它需要两个迭代器和一个函数指针作为参数。 所以,如果我想按字母顺序对字符串向量进行排序,我会这样做:
bool ascending(std::string lhs, std::string rhs) { return lhs < rhs; }
std::sort(my_vector.begin(), my_vector.end(), ascending);
问题是这种类型的sort函数区分大小写,因此在以大写“Z”开头的字符串后面会放置以小写“a”开头的字符串。 我看到的唯一可见解决方案是沿着bool ascending_case_insensitive()
创建一个额外的函数。 但是,如果我可以使用函数bool ascending()
并在sort中使用另外的bool is_case_sensitive
参数,那将是很好的。 这可能吗?
你现在的位置
bool ascending(std::string lhs, std::string rhs);
std::sort(my_vector.begin(), my_vector.end(), ascending);
你可以有
bool ascending(std::string lhs, std::string rhs, bool case_sensitive);
using namespace std::placeholders;
std::sort(my_vector.begin(), my_vector.end(), std::bind(ascending, _1, _2, false));
std::bind
的目的是返回一个对象,该对象在调用时调用绑定函数,可选地使用更改的参数。 您可以使用它来更改参数顺序,添加可选参数或将参数设置为特定的固定值。
由于std :: sort采用了比较仿函数的一个实例,因此您可以使用仿函数构造函数的参数来确定其行为。 例如,
class StringCompare
{
public:
StringCompare(bool is_case_sensitive=true) : is_case_sensitive(is_case_sensitive){}
bool operator()(const string&, const string&);///This would handle the comparison using the is_case_sensitive flag
private:
bool is_case_sensitive;
};
std::sort(my_vector.begin(), my_vector.end(), StringCompare(true));//case-sensitive comparison
std::sort(my_vector.begin(), my_vector.end(), StringCompare(false));//case-insensitive comparison
下面是一个示例,其中包含带有绑定额外参数的函数调用和通过值捕获额外参数的lambda表达式:
#include <iostream>// for std::cout
#include <vector>// for std::vector
#include <functional> // for std::bind
#include <algorithm> // for std::sort
bool ltMod(int i, int j, int iMod) {
return (i % iMod) < (j % iMod);
}
int main() {
std::vector<int> v = {3,2,5,1,4};
int iMod = 4;
std::cout << "\nExample for the usage of std::bind: ";
// _1 and _2 stand for the two arguments of the relation iMod is the bound parameter
std::sort(v.begin(),v.end(),std::bind(ltMod,std::placeholders::_1,std::placeholders::_2,iMod));
for( auto i : v ) std::cout << i << ',';
iMod = 3;
std::cout << "\nExample for lambda: ";
// lambdas are unnamed inplace functions
// iMod is captured by value. You can use the value within the function.
std::sort(v.begin(),v.end(),[iMod](int i, int j){ return ltMod(i,j,iMod); });
for( auto i : v ) std::cout << i << ',';
return 0;
}
/**
Local Variables:
compile-command: "g++ -std=c++11 test.cc -o a.exe"
End:
*/
以为我会回答我自己的问题,以便总结我得到的答案。 所以从我收集的内容来看,我基本上有两种选择。
第一个是写一个lambda函数来处理我的一次性案例。
// Lambda solution.
std::sort(my_vector.begin(), my_vector.end(),
[](std::string const &lhs, std::string const &rhs) // Thanks for optimizing my example code guys. No, seriously. ;)
{
return boost::toupper(lhs) < boost::toupper(rhs);
});
第二个更可重用的选项是创建一个仿函数来处理这样的排序情况。
// Functor solution.
class SortAscending
{
private:
bool _is_case_sensitive;
public:
SortAscending(bool is_case_sensitive) :
_is_case_sensitive(is_case_sensitive);
bool operator()(std::string const &lhs, std::string const &rhs)
{
if (_is_case_sensitive)
return boost::toupper(lhs) < boost::toupper(rhs);
else
return lhs < rhs;
}
};
std::sort(my_vector.begin(), my_vector.end(), SortAscending(false));
所以认为这几乎总结了我的选择?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.