The set of real numbers

class ASet { public: ASet(int size); // size - максимальное количество чисел в множестве ~ASet(); void incl(double x); // включить число в множество, // если такое число уже есть, то ничего не делать void excl(double x); // исключить bool contain(double x); // содержится в множестве? void print(); // распечатать состояние объекта }; 

Closed due to the fact that the participants of the question from Vlad from Moscow , 0xdb , VTT , mkkik , aleksandr barakin on March 19 at 13:10 are incomprehensible .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Moreover, this is not clear to us, since it is not clear how your class is implemented. - Vlad from Moscow
  • The class must have a field with a set. First, secondly the excl () method must accept a double? Here either the ordinal number in the set or double and throw an exception. If you redo your class, I think it will be obvious to you how to exclude a number. I advise you to immediately replace the constructor with ASet () and use std :: vector - jNX
  • If it is still not clear, I'll write something. - jNX
  • So what should be under the hood of your ASet ? A simple array, or std::vector , or std::set , or red-ebony, or ...? - HolyBlackCat
  • ASet should be a simple array "under the hood" - maks makarov

2 answers 2

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 
  • one
    It is a pity that a person will be too lazy to work out) and he needs this to pass the lab. But I put a plus sign ) - jNX
  • @jNX On the other hand, it consoles that for all sorts of std::copy( p, p + pos, std::ostream_iterator<double>( std::cout, " " ) ); On lab TSU precisely flies. :) - HolyBlackCat
  • @HolyBlackCat That's for sure) Although I would poroflil with an answer - jNX
  • @HolyBlackCat and even here it seems to me ASet::pos != ASet::n && std::find( p, p + pos, x ) == p + pos , a person who cannot write such a function will lose consciousness and abandon nafig with ++ - jNX pm
  • @jNX everything seems to be simple and clear. That is why only :: needed, the code does not become more beautiful with them - pavel

Like so.

 #include <iostream> using namespace std; class ASet { static const int MAX_C = 1000003; char isSet[MAX_C]; size_t hash(double x){ return std::hash<double>()(x) % MAX_C; } public: void incl(double x){ isSet[hash(x)] = 1; } void excl(double x){ isSet[hash(x)] = 0; } bool contain(double x){ return isSet[hash(x)]; } }; int main() { int N = 5; ASet set; for ( size_t i = 0; i < N; i++ ) set.incl( i ); for ( size_t i = 0; i < N+2; i++ ) cout << set.contain( i )<<" "; cout << endl; for ( size_t i = 0; i < N; i++ ) set.excl( i ); for ( size_t i = 0; i < N+2; i++ ) cout << set.contain( i )<<" "; cout << endl; return 0; } 

Conclusion.

 1 1 1 1 1 0 0 0 0 0 0 0 0 0