I have a list of STL vectors and I want to sort them by the first element of each vector

I have a list of vectors from STL. They have different lengths, and I want to sort them by the first element of each vector. For example, I have vectors: 4 4 5 6 10, 1 8 , 2 2 3 and 3 1 > 7 9. The first number is the size of each vector, followed by vector elements. I want to display vectors in a file sorted by the first element. In this example, my vectors should be displayed in the following order: 3 1 7 9, 2 2 3, 4 4 5 6 10 and 1 8 .

Here is my code:

ofstream fout ("retele.out");
fout << T << '\n';
for (i=1; i<=T; i++)
{
    fout << sol[i].size() << ' ';
    sort(sol[i].begin(),sol[i].end());
    for (j=0; j<sol[i].size(); j++)
        fout << sol[i][j] << ' ';
    fout << '\n';
}
fout.close();

"T" - . "" , , .

+4
6

: std::vector<T> operator< , ( cplusplus).

template < class T, class Alloc >
bool operator < (const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs);

:

( <) , lexicographic_compare, < ( , < b, b < a) .

,

sort(sol.begin(), sol.end());

UP: , std::vector, std::list<std::vector>. begin() end(), .

sort(sol, sol + SIZE);

SIZE - , .

+6

:

:

bool compare_vec (const std::vector<int>& first, const std::vector<int>& second)
{
  return first[1]<second[1];
}

:

sol.sort(compare_vec);
0

.

bool myfunction (vector<int> i,vector<int> j) { return (i[0]<j[0]);}
struct myclass {
  bool operator() (vector<int> i,vector<int> j) { return (i[0]<j[0]);}
} myobject;


...
ofstream fout ("retele.out");
fout << T << '\n';
for (i=1; i<=T; i++)
{
    fout << sol[i].size() << ' ';
    sort(sol[i].begin(),sol[i].end(), myfunction);
    for (j=0; j<sol[i].size(); j++)
        fout << sol[i][j] << ' ';
    fout << '\n';
}
fout.close();

, vectors.

0

:

// sort algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector

 bool myfunction (int i,int j) { return (i<j); }

struct myclass {
bool operator() (int i,int j) { return (i<j);}
} myobject;

 int main () {
 int myints[] = {32,71,12,45,26,80,53,33};
    std::vector<int> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33

  // using default comparison (operator <):
   std::sort (myvector.begin(), myvector.begin()+4);           //(12 32 45 71)26 80 53 33

         // using function as comp
    std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)

    // using object as comp
      std::sort (myvector.begin(), myvector.end(), myobject);     //(12 26 32 33 45 53 71 80)

    // print out content:
     std::cout << "myvector contains:";
      for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
      std::cout << ' ' << *it;
     std::cout << '\n';

    return 0;
  }
0

STL std::sort . , , :

std::sort(sol.begin(), sol.end(), [](const std::vector<int> &lhs, const std::vector<int> &rhs) { return lhs.front() < rhs.front(); });

, , .

If sol is not std::list, although the array is c-style std::vector<int> v[50001], as indicated in the comments. We need to rewrite it like this:

std::sort(sol[0], sol[nofElementsFilledIn], [](const std::vector<int> &lhs, const std::vector<int> &rhs) { return lhs.front() < rhs.front(); });
0
source
std::sort(MyVector.begin(), MyVector.end(), [](auto a, auto b) { return a.front() < b.front(); } );

I hope this can sort your vector

0
source

Source: https://habr.com/ru/post/1651064/


All Articles