sobota, 12 lipca 2025

std::sort() - C++ Builder


#include <vcl.h>
#include <algorithm>
#include <random>
#include <vector>

#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 Button1->Caption="&RUN";
 Button2->Caption="&CLOSE";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int i,j,k;
 struct Point
 {
int x;
int y;
 };
 std::vector<Point> date;
 std::random_device r1;
 std::mt19937 gen(r1());
 std::uniform_int_distribution<>dist(1,25);
 for(i=0;i<5;i++)
 {
date.push_back({dist(gen),dist(gen)});
 }
 j=0;
 for(const auto& temp1 : date)
 {
StringGrid1->Cells[j][0]=IntToStr(temp1.x);
StringGrid1->Cells[j][1]=IntToStr(temp1.y);
++j;
 }
 std::sort(begin(date),end(date),[] (const Point &v1, const Point &v2)
 {
return v1.y<v2.y;
 });
 j=0;
 for(const auto& temp2 : date)
 {
StringGrid2->Cells[j][0]=IntToStr(temp2.x);
StringGrid2->Cells[j][1]=IntToStr(temp2.y);
++j;
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 Close();
}