1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
#include <iostream>
#include <algorithm> // std::sort
#include <array>
#include <numeric>
#include <vector>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
using namespace std;
// ****** solution 1
struct MyStruct
{
int index;
int value;
};
std::vector<MyStruct> datas;
void sortByValue(std::vector<MyStruct> & tab){
std::sort(tab.begin(), tab.end(), [&](MyStruct const & a, MyStruct const & b){return a.value < b.value;});
}
void sortById(std::vector<MyStruct> & tab){
std::sort(tab.begin(), tab.end(), [&](MyStruct const & a, MyStruct const & b){return a.index< b.index;});
}
// ****** solution 2
std::array<int,5> numero={10,40,2,60,4};
std::array<int,5> ind;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
//*** solution 1
int numero[5];
for (int i = 0; i < 5; i++)
{
numero[0] = 10;
numero[1] = 40;
numero[2] = 2;
numero[3] = 60;
numero[4] = 4;
}
// tri des numeros
int i,j;
for (i = 1; i <5 ; ++i)
{
int elem = numero[i];
for (j = i; j > 0 && numero[j-1] > elem; j--)
numero[j] = numero[j-1];
numero[j] = elem;
}
for(int i=0;i<5;++i) datas.push_back({i, numero[i]});
sortByValue(datas); // Tri par valeur
ListBox1->Items->Add("*** Solution 1");
ListBox1->Items->Add("Tri par valeur");
for(auto const & d : datas){ ListBox1->Items->Add(IntToStr(d.index) + " " + IntToStr(d.value));
}
sortById(datas); // Tri par index
ListBox1->Items->Add("Tri par index");
for(auto const & d : datas){ ListBox1->Items->Add(IntToStr(d.index) + " " + IntToStr(d.value));
}
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button2Click(TObject *Sender)
{
//*** solution 2
// tri des numeros
int i,j;
for (i = 1; i <5 ; ++i)
{
int elem = numero[i];
for (j = i; j > 0 && numero[j-1] > elem; j--)
numero[j] = numero[j-1];
numero[j] = elem;
}
ListBox2->Items->Add("*** Solution 2");
struct {
bool operator()(int a, int b) const
{
return numero[a] < numero[b];
}
} customLess;
std::iota(ind.begin(), ind.end(), 0 ); // reçoit 0 1 2 3 4
std::sort(ind.begin() , ind.end() , customLess);
for(int i=0;i<5;i++)
ListBox2->Items->Add(IntToStr(ind[i]) + " " + IntToStr(numero[i]));
}
//--------------------------------------------------------------------------- |
Partager