0

First I had an int array called Myarray[51][4] filled by data.

For sorting it based on the second column of Myarray, I used the following code (using the conversion of array into a vector of vector: my_vector[51][4]) :

int Myarray [51][4];
vector< vector<int> > my_vector ;
for( const auto& row : Myarray ) 
    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] ; } ) ;

This code has sorted my_vector. Now I want to copy the sorted vector into Myarray again for using it as array of integer with dimension of [51][4]. How do I do it?

7
  • Just a quick question: Are you certain that you want to compare the second elements rather than the first ones? Commented Jul 30, 2013 at 12:48
  • 1
    Why don't you just sort the arrays directly? Commented Jul 30, 2013 at 12:55
  • 1
    int Myarray [51][4]; sort( begin(Myarray), end(Myarray), []( int(&a)[4], int(&b)[4] ) { return a[1] < b[1] ; } ) ; Commented Jul 30, 2013 at 12:56
  • 1
    @R.MartinhoFernandes did you try compiling that? It doesn't for me with g++ (4.7 and 4.8) Commented Jul 30, 2013 at 12:58
  • 1
    @stefan oh. Arrays are not assignable. Sigh. Commented Jul 30, 2013 at 13:07

1 Answer 1

4

The simplest and the most obvious one:

for (size_t row = 0; row < my_vector.size(); ++row) {
  for (size_t col = 0; col < my_vector[row].size(); ++col) {
    Myarray[row][col] = my_vector[row][col];
  }
}

Or another solution without the inner loop:

for (size_t row = 0; row < my_vector.size(); ++row) {
  copy(my_vector[row].begin(), my_vector[row].end(), Myarray[row]);
}

But better stop using C-style arrays in C++ and switch to std::vector or std::array completely!

For your request in the comment here is an example with std::vector:

vector<vector<int>> my_vector =  {{3, 8, 7, 2}, {9, 12, 0, 4}, {12, 2, 14, 1}};
sort(begin(my_vector), end(my_vector),
     [](const vector<int>& a, const vector<int>& b) { return a[1] < b[1]; }
) ;
Sign up to request clarification or add additional context in comments.

7 Comments

if I want to sort the following array what is the code?int Myarray[3][4] = { {3, 8, 7, 2}, {9, 12, 0, 4}, {12, 2, 14, 1}. I want to have: {12, 2, 14, 1} , {3, 8, 7, 2} , {9, 12, 0, 4}.
@MehdiTanhatalab, it is harder than I initially thought as you cannot copy array. You would have to write you own sort function. But better just stick with std containers, e.g. std::vector. Why do you prefer C-style arrays?
I have been programming using Visal Foxpro till now and a beginner in C++. There, there are only arrays but not vectors. That is why i know arrays better. bur if you recommend working with data using vector, as it may be more speed in the run time, I will shift to it.
@MehdiTanhatalab, in run-time the speed difference is almost negligible. And yes - I do recommend using std::vector or std::array instead of C-style arrays - in C++ you should want C-style arrays very-very rarely.
Thank you. I do not know what do you mean C-style array is there a C++-style array? Do you mean that why I omitted the "std::" ? I used the "using namespace std" which i did not bring here.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.