C++ Builder

La Isla Bonita



Sorting Vectors
Sorting Vectors


Description

This article illustrates the sort function which is used to sort the elemnts in a vector.
The sort function sorts the elements in a vector (or in a container) in increasing order (except when two or more values are the same)

For example, let's assume we have declared a vector as follows:
vector<int> Vector1;

Then, the sort function could have the following form:
sort(Vector1.begin(), Vector1.end());


For user defined types such as structures (instead of int), you need to define the compare function and pass it to the sort function as the third argument:

sort(Vector1.begin(), Vector1.end(), pCompare);

where pCompare is a pointer to a function used to compare the elements of the vector. An example of a Compare function is given in this example. Form 1 contains a TButton button.

SortVectors.cpp

#include <vcl.h>
#pragma hdrstop

#include "SortVectors.h"
#include 
#include 

using std::sort;
using std::vector;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

//Create 4 records
Data record1;
Data record2;
Data record3;
Data record4;
Data record5;

vector Vector;

//Compare function 
bool Compare(const Data& D1, const Data& D2)
{
  return D1.Percent > D2.Percent;
}

void SortVector(vector& V, bool (pfun)(const Data& D1, const Data& D2 ) )
{
    sort(V.begin(), V.end(), pfun);
}

void PrintVector( vector V )
{
    String S;
    
    for( vector::size_type i = 0 ; i != Vector.size(); i++)
    {
      S += String(V[i].Percent) + "\n";
    }

    ShowMessage( S );
}


//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    record1.Name = "Maria";
    record1.Percent = 1;
    record2.Name = "Andress";
    record2.Percent = 99;
    record3.Name = "Victor";
    record3.Percent = 80;
    record4.Name = "Alicia";
    record4.Percent = 3;
    record5.Name = "Rita";
    record5.Percent = 98;


    Vector.push_back(record2);
    Vector.push_back(record1);
    Vector.push_back(record3);
    Vector.push_back(record4);
    Vector.push_back(record5);

    SortVector( Vector, Compare );
    PrintVector( Vector );
}

SortVectors.h

#ifndef SortVectorsH
#define SortVectorsH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TButton *Button1;
        void __fastcall Button1Click(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------

//Data structure
struct Data
{
     int Percent;
     String Name;
};
#endif

Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.