在第一个向量元素的基础上对Structure进行排序

时间:2014-01-06 05:36:02

标签: c++ sorting vector struct

我有一个由两个矢量数组组成的结构。

struct hotel {
  vector<int> start_time[1000],end_time[1000];
};

我必须以start_time为基础对end_time进行排序。start_time[0] has 4 elements: start_time[0] = 12 10 8 9 end_time[0] = 100 20 30 50 start_time[1] has 5 elements: start_time[1] = 100 23 50 10 32 end_time[1] = 40 20 10 15 34 。 例如,

start_time[0] = 8 9 10 12
end_time[0]   = 30 50 20 100

start_time[1] = 10 23 32 50 100
end_time[1]   = 15 20 34 10 40

所以输出将是:

struct hotel {
    vector<int> start_time,end_time;
}h[1000];

请在这方面指导我。

谢谢

我发现了另外一件事,如果不是声明矢量arrarys我使用它:

{{1}}

也将服务于我的目的,但现在我有h [0]而不是start_time [0]和end_time [0]。 但有同样的问题如何排序h [i] .start_time而不是h [i] .end_time。 我试图想像托尼的解决方案,使用对。 谢谢你的回复。

2 个答案:

答案 0 :(得分:2)

#include <algorithm>

// create a container storing associated pairs of start and end times...
std::vector<std::pair<int,int>> times;

for (int v = 0; v < 1000; ++v) // vector to be ordered on this iteration...
{
    assert(my_hotel.start_time[v].size() == my_hotel.end_time[v].size());

    // populate times...
    for (int i = 0; i < my_hotel.start_time[v].size(); ++i)
        times.push_back(std::make_pair(my_hotel.start_time[v][i], my_hotel.end_time[v][i]));

    // sort it...
    std::sort(times.begin(), times.end());

    // copy sorted data back into hotel structure...
    for (int i = 0; i < times.size(); ++i)
    {
        my_hotel.start_time[v][i] = times[i].first;
        my_hotel.end_time[v][i] = times[i].second;
    }

    times.clear();
}

上述内容可以通过以下方式更具声明性地完成。 std::copy和lambdas,但我个人认为这样做并不重要。

答案 1 :(得分:0)

这是代码。

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, const char * argv[])
{
    const int vectorSize = 2;
    vector<int> start_time[vectorSize];
    vector<int> end_time[vectorSize];

    //element at index 0
    start_time[0] = {12, 10, 8, 9};
    end_time[0]   = {100, 20, 30, 50};

    //element at index 1
    start_time[1] = {100, 23, 50, 10, 32};
    end_time[1]   =  {40, 20, 10, 15, 34};

    //Here is what you need
    //Make sure that both start_time and end_time have same size, which will be in this case
    for(int i = 0; i < vectorSize; i++) //This will work on start_time, end_time indexes
    {
        //This will sort each vectore - I am using bubble sort method
        for(int v = 0; v < start_time[i].size(); v++)
        {
            for(int k = 0; k < start_time[i].size() - 1; k++)
            {
                if(start_time[i][k] > start_time[i][k + 1])
                {
                    int temp = start_time[i][k];
                    start_time[i][k] = start_time[i][k + 1];
                    start_time[i][k + 1] = temp;

                    int temp2 = end_time[i][k];
                    end_time[i][k] = end_time[i][k + 1];
                    end_time[i][k + 1] = temp2;
                }
            }
        }
    }

    for(int i = 0; i < vectorSize; i++) 
    {
        cout<<"start_time["<<i<<"]: ";
        for(int k = 0; k < start_time[i].size(); k++)
        {
            cout<<start_time[i][k]<<" ";
        }

        cout<<endl;

        cout<<"end_time["<<i<<"]: ";
        for(int k = 0; k < end_time[i].size(); k++)
        {
            cout<<end_time[i][k]<<" ";
        }

        cout<<endl;

    }



    return 0;
}