Hi,

This is a very basic question, but I can't seem to get working, I've a set of numbers that I need to display in the following, this is a class function btw.

The ouput should be something like
{ 1, 2, 3, 4, 5} (there are {} brackets and each element is seperated by a comma).

void set::display()const
{
          
        cout << "{";
        for (int i = 0; i < setlist.size(); i++)
            if (setlist.at(i) == true) 
               cout<< i <<",";           
        cout << "}";
            
}

Just to explain, there are 100 indexes, if a particular index is true, say for example, 6 is true, the output would display 6.

There is also another code which gives me the largest number of the set.

int set::getLargest() const
{
     for (int i = setlist.size()-1; i >= 0; i--)
     {
         if (setlist.at(i) == true)
         return i;
     } 
}

btw setlist is actually a private which is vector<bool>setlist.

My initial idea was to use the getLargest function (which is also a class function) into the if statement, so it would be
if ((setlist.at(i)==true) && setlist.at(i) != setlist.getLargest(setlist.at(i))
cout << i << ",";

However, the compiler would not let me do that.

Can someone point me into the right direction of getting my display function to work?

Thanks.

you don't really need the at() method, just index it just like an ordinary array if ((setlist[i]==true) && i != setlist.getLargest() Notice that method getLargest() does not take any parameters. Also note that getLargest() will return the index value not a value from the array.

I'm getting const class std::vector<bool, std::allocator<bool>' has no member named getLargest'

That's the main problem here I guess, using a class function within another one.

I'll post the header file here...

#ifndef SETH
#define SETH
// definition of a set of integers 
#include <iostream>
#include <vector>
using namespace std;
const int DEFAULTSIZE = 100;

class set
{
  public :
   set(); // default constructor - constructs an empty  set of integers in range 0 to DEFAULTSIZE -1
   set(int n);
     // pre : n >= 0 and n <= 500000
     // post : constructs an empty  set of integers in range 0 to n-1
   bool empty() const;
     // pre : none
     // post : returns true if set empty and otherwise false
   bool isIn(int x) const; 
     // pre : none
     // post : returns true if x is in set and otherwise false
   void dump() const;
     // pre : none
     // post : dumps out the valus in set (separated by white space) 
   int getCardinality() const;
     // pre : none
     // post :  returns the number of elements in the set
   void insert(int x);
     // pre : none
     // post : inserts x into this set (if in range)
   int getSmallest() const;
     // pre : set is not empty
     // post : returns smallest element in the set
   int getLargest() const;  
     // pre : set is not empty
     // post : returns largest element in the set
   set unions(int x) const;
     // pre : none
     // post : returns the union of this set and x (if x in range)
   set unions(const set &other) const;
     // pre : both  sets are in same range (domain)
     // post : returns the union of this set and other set
   set intersect(int x) const;
     // pre :  none
     // post : returns the intersection of this set and x (if x in range)
   set intersect(const set &other) const;
     // pre : both sets are in same range (domain)
     // post : returns the intersection of this set and other set
   void display()const;
     // pre : none
     // displays the elements of this set enclosed in braces and
     // separated by commas eg {1,2,3} or {} (do not use backspace, delete etc)
   set difference(const set &other) const;
     // pre :  both sets are in same range (domain)
     // post : returns the difference of this set and other set
   set difference(int x)const;
     // pre : none
     // post : returns the difference of this set and x (if x in range)
   int getUpperRange()const;
     // pre : none
     // post : returns the upper value in the domain (upper range)
   bool isEqual (const set & other) const;
     // pre : both sets are in the same range (domain)
     // post : returns true if this set is equal to other set
  private:
     vector <bool> setlist;  // indicates set elements as true
     int cardinality; // number of items in set
     
}; 
#endif

Thanks for your help though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.