在C ++中按代理排序(或:按照另一个的内容排序一个容器) [英] Sort by proxy (or: sort one container by the contents of another) in C++

查看:110
本文介绍了在C ++中按代理排序(或:按照另一个的内容排序一个容器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组数据被分成两个数组(我们称之为 data keys )。也就是说,对于索引 i 的任何给定项,我可以使用 data [i] keys [i] 为该项目的键。我不能改变这个结构(例如,把键和数据交织到一个数组),因为我需要传递数据数组到一个库函数,期望一个特定的数据布局。

I have a set of data which is split into two arrays (let's call them data and keys). That is, for any given item with an index i, I can access the data for that item with data[i] and the key for that item with keys[i]. I cannot change this structure (eg, to interleave keys and data into a single array), because I need to pass the data array to a library function which expects a certain data layout.

如何根据数组的内容对两个数组进行排序(最好使用标准库函数) ?

How can I sort both arrays (preferably using standard library functions) according to the content of the keys array?

推荐答案

事实证明,Boost包含一个迭代器,它几乎可以实现 paired_iterator 来自我的其他答案

It turns out that Boost contains an iterator which does pretty much what the paired_iterator from my other answer does:

Boost.Iterator Zip迭代器

这似乎是最好的选择。

这篇关于在C ++中按代理排序(或:按照另一个的内容排序一个容器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆