我怎么排序一个std ::矢量由不同的std ::向量的价值观? [英] How do I sort a std::vector by the values of a different std::vector?

查看:130
本文介绍了我怎么排序一个std ::矢量由不同的std ::向量的价值观?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个的std ::矢量,都是一样的长度。我要排序这些载体之一,和相同的变换应用到所有其他载体。是否有这样做的一个非常简洁的方式? (preferably使用STL或升压)?一些载体持有 INT 和他们中的一些的std ::字符串

伪code:

 的std ::矢量<&INT GT;指数= {3,1,2};
的std ::矢量<标准::字符串>值= {第三,第一,第二};转型=排序(索引);
索引是现在{1,2,3};......奇迹发生的转换应用于值...
值现在{第一,第二,第三};


解决方案

在与你再加夫里奥尔的做法是好的。首先,建立一个由数字1的矢量... N 的,以及从矢量支配排序顺序的元素:

 的typedef矢量<&INT GT; ::为const_iterator myiter;矢量<&一双LT;为size_t,myiter> >顺序(Index.size());为size_t N = 0;
对于(myiter IT = Index.begin();它= Index.end(!); ++中,++ N)
    为了[N] = make_pair(N,吧);

现在你可以使用自定义排序分拣机本阵:

 结构排序{
    布尔运算符()(对<为size_t,myiter>常量和放大器;一,对<为size_t,myiter>常量和b){
        回*(a.second)LT; *(b.second);
    }
};排序(order.begin(),order.end(),排序());

现在你捕获重排的顺序里面订单(更多precisely,在项目的第一部分)。现在,您可以使用此排序到其他媒介进行排序。有可能是在同时运行一个非常聪明的就地变种,但直到别人来了吧,这里有一个变种,是不是在原地。它采用订单为每个元素的新指数查表。

 模板< typename的T>
矢量< T> sort_from_ref(
    矢量< T>常量和放大器;在,
    矢量<&一双LT;为size_t,myiter> >常量和放大器;参考
){
    矢量< T> RET(in.size());    为size_t常量大小= in.size();
    用于(为size_t我= 0; I<大小; ++ I)
        RET [i]于= [参考文献[I]。首先]    返回RET;
}

I have several std::vector, all of the same length. I want to sort one of these vectors, and apply the same transformation to all of the other vectors. Is there a neat way of doing this? (preferably using the STL or Boost)? Some of the vectors hold ints and some of them std::strings.

Pseudo code:

std::vector<int> Index = { 3, 1, 2 };
std::vector<std::string> Values = { "Third", "First", "Second" };

Transformation = sort(Index);
Index is now { 1, 2, 3};

... magic happens as Transformation is applied to Values ...
Values are now { "First", "Second", "Third" };

解决方案

friol's approach is good when coupled with yours. First, build a vector consisting of the numbers 1…n, along with the elements from the vector dictating the sorting order:

typedef vector<int>::const_iterator myiter;

vector<pair<size_t, myiter> > order(Index.size());

size_t n = 0;
for (myiter it = Index.begin(); it != Index.end(); ++it, ++n)
    order[n] = make_pair(n, it);

Now you can sort this array using a custom sorter:

struct ordering {
    bool operator ()(pair<size_t, myiter> const& a, pair<size_t, myiter> const& b) {
        return *(a.second) < *(b.second);
    }
};

sort(order.begin(), order.end(), ordering());

Now you've captured the order of rearrangement inside order (more precisely, in the first component of the items). You can now use this ordering to sort your other vectors. There's probably a very clever in-place variant running in the same time, but until someone else comes up with it, here's one variant that isn't in-place. It uses order as a look-up table for the new index of each element.

template <typename T>
vector<T> sort_from_ref(
    vector<T> const& in,
    vector<pair<size_t, myiter> > const& reference
) {
    vector<T> ret(in.size());

    size_t const size = in.size();
    for (size_t i = 0; i < size; ++i)
        ret[i] = in[reference[i].first];

    return ret;
}

这篇关于我怎么排序一个std ::矢量由不同的std ::向量的价值观?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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