If the data can be stored in a disordered form, then the implementation of the incl
method may look like this, as shown below in the simplified demonstration program.
#include <iostream> #include <algorithm> #include <iterator> class ASet { public: ASet( size_t size ); // size - максимальное количество чисел в множестве ~ASet(); void incl(double x); // включить число в множество, // если такое число уже есть, то ничего не делать void print() const; // распечатать состояние объекта private: size_t n; size_t pos; double *p; }; ASet::ASet( size_t size ) : n( size ), pos( 0 ), p( new double[n] ) { } ASet::~ASet() { delete [] p; } void ASet::incl( double x ) { if ( pos != n && std::find( p, p + pos, x ) == p + pos ) { p[pos++] = x; } } void ASet::print() const { std::copy( p, p + pos, std::ostream_iterator<double>( std::cout, " " ) ); } int main() { const size_t N = 10; ASet set( N ); for ( size_t i = 0; i < N; i++ ) { set.incl( i ); set.print(); std::cout << '\n'; } return 0; }
The output of the program to the console:
0 0 1 0 1 2 0 1 2 3 0 1 2 3 4 0 1 2 3 4 5 0 1 2 3 4 5 6 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 9
ASet
? A simple array, orstd::vector
, orstd::set
, or red-ebony, or ...? - HolyBlackCat