根据一个的排列对三个向量进行排序,并重复。 [英] Sorting Three vectors based on the arrangement of one, with duplicates.

查看:87
本文介绍了根据一个的排列对三个向量进行排序,并重复。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  vector< int>参考=年龄; 
sort(ages.begin(),ages.end());
for(int i = 0; i <20; ++ i){{b $ b for(int j = 0; j <20; ++ j){
if(ages [i] == reference [j]){
cout<< setw(LongestString(names))< <名称[j]<< setw(agewidth)<< age [i]< setw(probwidth)<< prob [j]<< ; endl;
}
}
}

所以我有三个向量我想根据年龄对它们进行排序,然后重新排序。但是,在各个时代都有重复,因此每当重复出现时,它都会多次打印。有没有一种方法可以防止这种情况而无需创建结构?

解决方案

生成从0到ages.size()-1的索引向量。根据年龄对索引进行排序,可能使用lambda比较功能。然后根据排序的索引向量对所有三个向量重新排序。



如果仅根据排序的索引复制一个向量,则重新排序很简单,这将使所有三个向量保持同步。



=原始[sorted_index [i]]。如下面的示例所示,也可以就地重新排序,但这是一个鲜为人知的算法,如果这是家庭作业,我不知道应该在哪里让学生知道该算法或能够找到此算法的示例。该算法基于以下事实:可以将置换视为一系列循环:



http://en.wikipedia.org/wiki/Permutation#Cycle_notation

  std :: vector< int>一种; //年龄
std :: vector< std :: string> N; //命名
std :: vector< int> Z; //邮政编码
std :: vector< size_t>一世; //索引

//向量A,N,Z在此初始化...

//初始化索引
的向量for(size_t i = 0; i< A.size(); i ++)
I.push_back(i);
//根据A对索引进行排序的向量
std :: stable_sort(I.begin(),I.end(),
[& A](size_t i,size_t j) {返回
A [i]< A [j];});
//将A,N,Z重新排序也将I恢复为0到size-1
//时间复杂度为O(n)
//每次移动都会在其元素中放置一个元素排序位置
for(size_t i = 0; i< A.size(); i ++){
size_t j,k;
int tA; //临时变量
std :: string tN;
int tZ;
if(i!= I [i]){
tA = A [i];
tN = N [i];
tZ = Z [i];
k = i;
while(i!=(j = I [k])){
A [k] = A [j];
N [k] = N [j];
Z [k] = Z [j];
I [k] = k;
k = j;
}
A [k] = tA;
N [k] = tN;
Z [k] = tZ;
I [k] = k;
}
}


vector<int> reference = ages;
sort(ages.begin(),ages.end());
for (int i=0; i<20 ;++i){
    for (int j=0; j<20; ++j){
        if (ages[i]==reference[j]){
            cout << setw(LongestString(names))<<names[j] <<setw(agewidth)<<ages[i]<<setw(probwidth)<<prob[j]<<endl;
        }
    }
}

So I have a three vectors and I want to sort them based off of the ages, and then reorder them. However, there are repetitions in the ages, so whenever it gets to those it prints them multiple times. Is there a way to prevent that without the creation of a structure?

解决方案

Generate a vector of indices from 0 to ages.size()-1. Sort the indices according to ages, probably using a lambda compare function. Then reorder all three vectors according to the sorted vector of indices. This will keep all three vectors in "sync", regardless of any duplicates in ages.

The reordering is simple if you just copy a vector according to indices sorted[i] = original[sorted_index[i]]. Reordering in place can also be done, as shown in the example below, but it's a little known algorithm, and if this is homework, I don't know where a student would be expected to be aware of this algorithm or be able to find examples of this algorithm. The algorithm is based on the fact that a permutation can be considered as a sequence of cycles:

http://en.wikipedia.org/wiki/Permutation#Cycle_notation

    std::vector <int> A;                // ages
    std::vector <std::string> N;        // names
    std::vector <int> Z;                // zip codes
    std::vector <size_t> I;             // indices

    // vectors A, N, Z are initialized here ...

    // initialize vector of indices
    for(size_t i = 0; i < A.size(); i++)
        I.push_back(i);
    // sort vector of indices according to A
    std::stable_sort(I.begin(), I.end(),
        [&A](size_t i, size_t j) {return 
        A[i] < A[j];});
    // reorder A, N, Z in place also restore I back to 0 to size-1
    // time complexity is O(n)
    // every move places an element in its sorted position
    for(size_t i = 0; i < A.size(); i++){
        size_t j, k;
        int tA;                         // temp variables
        std::string tN;
        int tZ;
        if(i != I[i]){
            tA = A[i];
            tN = N[i];
            tZ = Z[i];
            k = i;
            while(i != (j = I[k])){
                A[k] = A[j];
                N[k] = N[j];
                Z[k] = Z[j];
                I[k] = k;
                k = j;
            }
            A[k] = tA;
            N[k] = tN;
            Z[k] = tZ;
            I[k] = k;
        }
    }

这篇关于根据一个的排列对三个向量进行排序,并重复。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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