近值排序

近值排序

report from :https://codereview.stackexchange.com/questions/48470/std-lib-like-c-function-to-find-nearest-elements-in-a-container


// Distance
// ========

template <typename T>
struct Distance {
	T operator () (const T& a, const T&  b) {
		return std::abs(a - b);
	}
};

// Compare Distance
// ================

template <
	typename T,
	typename DistanceFunctor = Distance<T>,
	typename CompareFunctor = std::less<decltype(
		std::declval<DistanceFunctor>()(std::declval<T>(), std::declval<T>()))>>
struct CompareDistance
{
	T pivot;
	DistanceFunctor distance;
	CompareFunctor compare;
	CompareDistance(T&& pivot)
		: pivot(std::move(pivot))
	{}

	CompareDistance(T&& pivot, DistanceFunctor&& distance)
		: pivot(std::move(pivot)),
		distance(std::move(distance))
	{}

	CompareDistance(T&& pivot, DistanceFunctor&& distance, CompareFunctor&& compare)
		: pivot(std::move(pivot)),
		distance(std::move(distance)),
		compare(std::move(compare))
	{}

	bool operator () (const T& a, const T& b) {
		return compare(distance(a, pivot), distance(b, pivot));
	}
};

// Distance Sort
// =============

template <typename Iterator, typename T>
inline void distance_sort(
	Iterator first,
	Iterator last,
	T&& pivot)
{
	typedef typename std::iterator_traits<Iterator>::value_type value_type;
	CompareDistance<value_type> compare_distance(std::move(pivot));
	std::sort(first, last, compare_distance);
}

template <typename Iterator, typename T, typename Distance>
inline void distance_sort(
	Iterator first,
	Iterator last,
	T&& pivot,
	Distance&& distance)
{
	typedef typename std::iterator_traits<Iterator>::value_type value_type;
	CompareDistance<value_type, Distance> compare_distance(
		std::move(pivot),
		std::move(distance));
	std::sort(first, last, compare_distance);
}

template <typename Iterator, typename T, typename Distance, typename Compare>
inline void distance_sort(
	Iterator first,
	Iterator last,
	T&& pivot,
	Distance&& distance,
	Compare&& compare)
{
	typedef typename std::iterator_traits<Iterator>::value_type value_type;
	CompareDistance<value_type, Distance, Compare> compare_distance(
		std::move(pivot),
		std::move(distance),
		std::move(compare));
	std::sort(first, last, compare_distance);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现基金净值排序,可以按照以下步骤进行: 1. 定义基金类,包括基金代码、基金名称、基金净值等属性。 2. 创建基金对象,并将基金对象存储到集合中。 3. 定义一个排序方法,使用 Collections.sort() 方法对基金集合进行排序。 4. 在排序方法中实现 Comparator 接口,重写 compare() 方法,根据基金净值进行排序。 下面是一个示例代码: ```java import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class FundSorter { public static void main(String[] args) { List<Fund> funds = new ArrayList<Fund>(); funds.add(new Fund("001", "基金1", 1.5)); funds.add(new Fund("002", "基金2", 2.0)); funds.add(new Fund("003", "基金3", 1.8)); funds.add(new Fund("004", "基金4", 1.2)); funds.add(new Fund("005", "基金5", 2.5)); Collections.sort(funds, new Comparator<Fund>() { public int compare(Fund f1, Fund f2) { return Double.compare(f2.getNetValue(), f1.getNetValue()); } }); for (Fund fund : funds) { System.out.println(fund.getCode() + " " + fund.getName() + " " + fund.getNetValue()); } } } class Fund { private String code; private String name; private double netValue; public Fund(String code, String name, double netValue) { this.code = code; this.name = name; this.netValue = netValue; } public String getCode() { return code; } public String getName() { return name; } public double getNetValue() { return netValue; } } ``` 输出结果为: ``` 005 基金5 2.5 002 基金2 2.0 003 基金3 1.8 001 基金1 1.5 004 基金4 1.2 ``` 这里使用了 Collections.sort() 方法对基金集合进行排序,传入了一个 Comparator 对象作为参数,重写了 compare() 方法,根据基金净值进行排序。最后输出排序后的基金信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值