簡體   English   中英

用兩個向量排序

[英]Sorting with two vectors

我想知道是否有可能,例如,你有一個vector<string>和一個帶有相應對的vector<double> ,按字母順序對vector<string>排序,同時保持對匹配。

我知道這可以通過創建一個包含兩個值並只對其進行排序的類來完成,但我寧願保留兩個單獨的向量。

有任何想法嗎?

最終守則:

#include "std_lib_facilities.h"

struct Name_pairs
{
       vector<string>names;
       vector<double>ages;
       void quicksort(vector<string>& num, vector<double>& num2, int top, int bottom);
       int divide(vector<string>& array, vector<double>& array2, int top, int bottom);
       bool test();
       string read_names();
       double read_ages();
       void print();
};

string Name_pairs::read_names()
{
       string name;
     cout << "Enter name: ";
     cin >> name;
     names.push_back(name);
     return name;
}

double Name_pairs::read_ages()
{
     double age;
     cout << "Enter corresponding age: ";
     cin >> age;
     ages.push_back(age);
     cout << endl;
     return age;
}

int Name_pairs::divide(vector<string>& array, vector<double>& array2, int top, int bottom)
{
    string x = array[top];
    int i = top-1;
    int j = bottom+1;
    string temp;
    double temp2;
    do{
        do
        {
             j--;
             }while(x<array[j]);

        do
        {
             i++;
             }while(x>array[i]);

        if(i<j)
        {
               temp = array[i];
               temp2 = array2[i];
               array[i] = array[j];
               array2[i] = array2[j];
               array[j] = temp;
               array2[j] = temp2;
               }
               }while(i<j);
        return j;
}


void Name_pairs::quicksort(vector<string>& num, vector<double>& num2, int top, int bottom) // top is subscript of beginning of vector
{
     int middle;
     if(top < bottom)
     {
            middle = divide(num, num2, top, bottom);
            quicksort(num, num2, top, middle);
            quicksort(num, num2, middle+1, bottom);
            }
     return;
}

void Name_pairs::print()
{
     for(int i = 0; i < (names.size()-1) && i < (ages.size()-1); ++i)
             cout << names[i] << " , " << ages[i] << endl;
}

int main(){
    Name_pairs np;
    cout << "Enter names and ages. Use 0 to cancel.\n";
    bool finished = false;
    while(!finished){
    finished = "0" == np.read_names();
    finished = 0 == np.read_ages();}
    np.quicksort(np.names, np.ages, 0, (np.names.size()-2));
    np.print();
    keep_window_open();}

如果您自己手動對它們進行排序,則只需將雙數組中的相應項目與通常執行的字符串數組中的項目交換即可。 或者,您可以擁有第三個數組:

vector<unsigned int> indices;

這只是索引到字符串/ double數組,而是對該數組進行排序(根據字符串數組中的值進行交換)。

你可以創建一個輔助向量:

vector<unsigned int> indices;

將其初始化為0,1,2,...n-1 ,其中n是向量的大小,並使用sort算法使用看待vector<string>的仿函數對其進行排序,即,當被要求時比較index1和index2,仿函數將查找相應的字符串並進行比較。 一旦對indices進行了排序,就可以輕松地對兩個數組進行排序,使其在線性時間內符合要求。

編輯:我沒有看到吉姆巴克的第二個建議。 我的答案只是一個擴展版本。

雖然索引的想法實際上是相同的,但事實上你可以定義一個知道兩個向量的隨機訪問迭代器類型,並將它們(通過賦值)同步移動。 該迭代器的value_type將是pair。 在函數式編程中,你稱之為“zip”(但不是拉鏈)。

除非你在太空中非常緊張,否則絕對不值得麻煩。 如果你有空間,實際上將兩個向量壓縮成單個向量對,或使用索引方法,都更容易。

如果你能夠在第一時間做到正確,那么復制/粘貼10行快速排序並進行明顯的修改將是你想要的最快速的方式。


編者注: 在Boost中有一個已經編寫過的Zip Iterator ,正如這個問題的新答案所指出的: “鎖定”兩個向量並對它們進行排序

如果您打算使用std :: sort,則需要使用像對一樣的數據結構。 當然,您可以手動對向量進行排序,並基本上重新實現std :: sort。

這個問題實際上取決於許多其他問題,例如:

  • 向量中有多少項?
  • 性能有多重要?
  • 你真的想要實現自己的排序算法嗎?

實現快速排序應該相當輕松,並且可以避免移動數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM