使用vector实现argsort

template <typename T>
void vec_print(string vec_name,vector<T> &vec ){
	cout<<vec_name<<".size()="<<vec.size()<<endl;
	cout<<vec_name<<":\n"<<endl;
	//print vector 
	for (auto val : vec){
	    cout << val << " ";
	}
	cout<<endl;
}  

template <typename T> 
vector<int> argsort(const std::vector<T>& array)
{
	const int array_len(array.size());
	std::vector<int> array_index(array_len, 0);
	for (int i = 0; i < array_len; ++i)
		array_index[i] = i;

	std::sort(array_index.begin(), array_index.end(),
		[&array](int pos1, int pos2) {return (array[pos1] < array[pos2]);});

	return array_index;
}

vec_print("vec_peak_heights", vec_peak_heights ); 
vector<int> sort_index =  argsort(vec_peak_heights);
vec_print("vec_peak_heights", vec_peak_heights ); 
vec_print("sort_index", sort_index ); 
#result:
vec_peak_heights.size()=54
vec_peak_heights:

1122 1255 1030 1070 1347 1232 1196 1313 1169 1265 1240 1326 1301 1216 1140 1298 1384 1281 1304 1239 1503 1294 1240 1472 1484 1528 1485 554 1265 1094 1397 1095 1318 1272 1414 1209 1265 1380 1463 1427 1570 1623 1538 1415 1299 1469 1306 1504 1280 1074 821 367 255 355
sort_index.size()=54
sort_index:

52 53 51 27 50 2 3 49 29 31 0 14 8 6 35 13 5 19 22 10 1 9 36 28 33 48 17 21 15 44 12 18 46 7 32 11 4 37 16 30 34 43 39 38 45 23 24 26 20 47 25 42 40 41
sorted values:
 255 355 367 554 821 1030 1070 1074 1094 1095 1122 1140 1169 1196 1209 1216 1232 1239 1240 1240 1255 1265 1265 1265 1272 1280 1281 1294 1298 1299 1301 1304 1306 1313 1318 1326 1347 1380 1384 1397 1414 1415 1427 1463 1469 1472 1484 1485 1503 1504 1528 1538 1570 1623
v
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值