Description
This article illustrates the sort function which is used to sort
the elemnts in a vector. SortVectors.cpp SortVectors.h Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.
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.
#include <vcl.h>
#pragma hdrstop
#include "SortVectors.h"
#include
#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