用两个向量排序 [英] Sorting with two vectors

查看:169
本文介绍了用两个向量排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果你有一个向量< string> 向量< double> 与对应的对,按照字母顺序对向量< string> 进行排序,同时保持对匹配。



我知道这可以通过创建一个类来保存这两个值,只是排序,但我宁愿保持两个单独的向量。



任何想法? p>

最终代码:

  #includestd_lib_facilities.h

struct Name_pairs
{
vector< string> names;
矢量< 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<< 输入名字: ;
cin>>名称;
names.push_back(name);
return name;
}

double Name_pairs :: read_ages()
{
double age;
cout<< 输入相应年龄:;
cin>>年龄;
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
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 return j;
}


void Name_pairs :: quicksort(vector< string& num,vector< double>& num2,int top,int bottom)// top是下标的向量
{
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]<< ,< age [i]< endl
}

int main(){
Name_pairs np;
cout<< 输入名称和年龄。使用0取消。
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();}


解决方案

打算使用std :: sort,你将需要使用一对数据结构。
当然,你可以手动排序向量,并基本上重新实现std :: sort。



这个问题真的取决于很多其他问题,例如:




  • 向量中有多少项?

  • 性能有多重要?

  • 您真的想实现自己的排序算法吗?



实现快速排序相当无痛,并且将允许您避免移动数据。


I'm wondering if it's possible if you have, for example, a vector<string> and a vector<double> with corresponding pairs, to sort the vector<string> alphabetically while keeping the pairs matched up.

I know this can be done by creating a class that holds both values and just sorting that, but I'd rather keep two separate vectors.

Any ideas?

Final Code:

#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();}

解决方案

If you intend to use std::sort, you will need to use a datastructure like a pair. You can, of course, manually sort the vectors, and essentially reimplement std::sort.

This question really depends on a great number of other questions, such as:

  • How many items will be in the vectors?
  • How critical is performance?
  • Do you REALLY want to implement your own sort algorithm?

Implementing a quicksort should be fairly painless, and will allow you to avoid moving the data around.

这篇关于用两个向量排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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