2013-07-30 59 views
0

我没有通过将整数数组转换为向量来排序整数数组。排序整数数组的数组

#include<iostream> 
#include<algorithm> 
//#include <sstream> 
#include <vector> 
#include <iterator> 
#include <iomanip> 

using namespace std ; 
int kj; 
int aRawdata [3] [4] = {{1,0,37,52},{2,0,49,49}, {3,0,52,64}}; 
int aSolution[3] [4]; 
int main() 
{ 

//copy aRawdata to aSolution 
    copy(&aRawdata[0][0], &aRawdata[0][0] + 3*4, &aSolution[0][0]);  

    // insering a random number into the second column of aSolution; the column which would be base of the sort 
    for (kj = 0 ; kj < 3 ; kj++) 
    {  
     aSolution [kj] [1] = rand(); 
    } 
    // converting aSolution into vector (my_vector)  
     {// start sort using the vectors 
     vector< vector<int> > my_vector ; 
     for(const auto& row : aSolution) my_vector.push_back(vector<int>(begin(row), end(row))) ; 
     sort(begin(my_vector), end(my_vector), 
        [](const vector<int>& a, const vector<int>& b) { return a[1] < b[1] ; }) ;  
     // for Copying a “vector of vector” into“ array of array” 
     for (size_t row = 0; row < my_vector.size(); ++row) { 
      copy(my_vector[row].begin(), my_vector[row].end(), aSolution[row]); 
}  // print 
     for(const auto& row : aSolution) 
     { 
      for(int v : row) cout << setw(10) << v ; 
      cout << '\n' ; 
     } 

    } 
} 

我有两个问题。

  1. 如何将my_vector(这是一个排序向量)的数据再次复制到aSolution中导致对aSolution数组进行排序?
  2. 如何在不使用矢量的情况下直接对aSolution进行排序? (排序将基于aSolution的第二列)。 此致敬礼。
+1

aSolution是int,这是你的真实代码吗?这不能被编译。另外,做适当的缩进,这段代码是不可读的。 –

+0

@对不起,我在主要问题中替换了整个代码。 –

回答

0

向量的数据在内存中对齐(像一个大数组),所以只要确保它的大小合适,就可以将其重新存回。

至于你的第二个问题,你自己实现快速排序 - 它需要5个月。 =)否则,如果没有适当的容器,我认为你不能使用std的排序。