Generic vector sort (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example demonstrates how to sort a generic vector in a C++ application. This example uses the Standard Template Library (STL) headers <vector> and <algorithm>.

The example contains three files:

  • Shapes.h - Declares classes that describe the following geometric shapes: Triangle, Rectangle, Square.
  • Shapes.cpp - Contains the implementation of the classes declared in Shapes.h.
  • Main.cpp - Main file; Declares a vector of shapes, adds some shapes to the vector, then sorts the shapes in the vector by their area.

Code

  • Main.cpp
#pragma hdrstop
#pragma argsused

#include <tchar.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
#include "Shapes.h"

using namespace std;

// prints the area for each element of the vector
void PrintVector(vector<Shape*>vect) {
#ifdef _WIN64
	for (auto i = vect.begin(); i < vect.end(); i++) {
#else
	for (vector<Shape*>::iterator i = vect.begin(); i != vect.end(); ++i) {
#endif
		cout << (*i)->GetArea() << " ";
	}
	cout << endl;
}

#ifdef _WIN32

// compare function for 32bit applications
bool compareFunction(Shape* left, Shape* right) {
	int al = left->GetArea(), ar = right->GetArea();
	return (al < ar);
}
#endif

int _tmain(int argc, _TCHAR* argv[]) {
	// declare a vector of Shape*
	vector<Shape*>v;

	// add three different shapes:
	// a rectangle, a triangle and a square
	v.push_back(new Rectangle(3, 5));
	v.push_back(new Triangle(1, 2, 3));
	v.push_back(new Square(3));

	// print the vector
	cout << "Unsorted vector:";
	PrintVector(v);

	// sort the vector
	// the compare function is defined as a lambda expression
#ifdef _WIN64
	sort(v.begin(), v.end(), [](Shape * left, Shape * right)->bool
	{int al = left->GetArea(), ar = right->GetArea(); return (al < ar);});
#else
	sort(v.begin(), v.end(), compareFunction);
#endif
	// print the vector again to check if the areas are sorted
	cout << "Sorted vector:";
	PrintVector(v);

	cin.get();

	return 0;
}
  • Shapes.h:
// ---------------------------------------------------------------------------

#ifndef ShapesH
#define ShapesH

// ---------------------------------------------------------------------------
// base class for shapes
class Shape {
public:
	// pure virtual function
	// in derived classes, GetArea returns the area of the shape
	virtual double GetArea() = 0;
};

// class that describes a triangle shape
class Triangle : public Shape {
private:
	// the length of the sides
	double x, y, z;

public:
	// triangle constructor
	Triangle(double x, double y, double z);
	// returns the area of the triangle
#ifdef _WIN64
	double GetArea() override;
#else
	double GetArea();
#endif
};

// class that describes an rectangle shape
class Rectangle : public Shape {
private:
	// the length of the sides
	double lg, lat;

public:
	// rectangle constructor
	Rectangle(double lg, double lat);
	// returns the area of the rectangle
#ifdef _WIN64
	double GetArea() override;
#else
	double GetArea();
#endif
};

// class that describes an square shape
class Square : public Rectangle {
public:
	// square constructor
	// calls the rectangle shape constructor having the lengths of the two different sides the same
	Square(double lat) : Rectangle(lat, lat) {
	}
};
#endif
  • Shapes.cpp
#pragma hdrstop

#include "Shapes.h"
#include <math.h>

// Triangle constructor
Triangle::Triangle(double x, double y, double z) {
	this->x = x;
	this->y = y;
	this->z = z;
}

// returns the triangle area
// uses Heron's formula
double Triangle::GetArea() {
	double s = (x + y + z) / 2;
	return sqrt(s*(s - x)*(s - y)*(s - z));
}

// Rectangle constructor
Rectangle::Rectangle(double lg, double lat) {
	this->lg = lg;
	this->lat = lat;
}

// returns the rectangle area
double Rectangle::GetArea() {
	return (lg * lat);
}
#pragma package(smart_init)

Uses

See Also