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' ;
}
}
}
我有兩個問題。
- 如何將my_vector(這是一個排序向量)的數據再次複製到aSolution中導致對aSolution數組進行排序?
- 如何在不使用矢量的情況下直接對aSolution進行排序? (排序將基於aSolution的第二列)。 此致敬禮。
aSolution是int,這是你的真實代碼嗎?這不能被編譯。另外,做適當的縮進,這段代碼是不可讀的。 –
@對不起,我在主要問題中替換了整個代碼。 –