One of the types of data structures that are the direct embodiment of mathematical entities in computer science are sets. Operations with them quite often underlie various algorithms. Different programming languages have their own means for describing sets.
Necessary
- - development environment;
- - translator from the selected programming language.
Instructions
Step 1
Describe the set using the programming language, if available. For example, in Pascal there is a set construct that allows you to declare the corresponding types. True, the volume of such sets should not exceed 256 elements. An example of set type declarations might look like this:
type
AZLetters = set of 'A'.. 'Z';
AllLetters = set of char;
Variables and constants of types that are sets are declared in the usual way. In this case, set literals can be used for initialization. For example:
const
LettersSet1: AZLetters = ['A', 'B', 'C'];
Step 2
Use the capabilities of standard libraries or modules to describe sets. So, the C ++ template library, which should be supplied with the compiler, includes a template for the set container class that implements the functionality of sets:
template <
class Key, class Traits = less, class Allocator = allocator
class set
As you can see from the listing, the arguments of the set template are: the data type of the elements of the set, the type of the functional object for determining the order of the elements in the set, and the type of the memory allocator. In this case, only the first argument is required (as the other two, the standard binary predicate less and the standard allocator are used by default).
Step 3
Apply classes or class templates used in the development of frameworks that implement the functionality of working with sets, if any. An example of such a tool is the QSet template class of the QtCore module of the Qt library. Its capabilities are similar to those of the STL set container described in the previous step.
Step 4
Describe the set using your own implementation means. Use bit flags, stored in fixed length arrays, for sets of elements of simple types and small size. Implement a set container class for complex data types. As a basis, you can take the functionality of associative or hashing associative arrays. It, in turn, can be built on the basis of self-balancing binary search trees (for example, red-black trees).