Hey guys.

These functions are not working together correctly because of the true/false statements. Can someone please show me how I can fix the empty() function to work simultaneously with my other function provided? :S

My getSmallest() function will not return the value due to the empty() function..

bool set::empty() const
{
     for (int i = 0; i <= positions.size(); i++)
     {
         if (i = -1)
         {
               return true;
         }
         return false;
     }
}

int set::getSmallest() const
{
    for (int i = 0; i < DEFAULTSIZE; i++)
    {
        if (setlist.at(i) == 1)
        {
           positions.push_back(i);
        }
    }
    
    if ( !positions.empty() )
    {
    	return *( min_element( positions.begin(), positions.end() ) );
    }
    return -1;
}

Thanks :)

Recommended Answers

All 4 Replies

your problem is here:

if (i = -1)

the condition is always true
try using

if (i == -1)

your problem is here:

if (i = -1)

the condition is always true
try using

if (i == -1)

also see

for (int i = 0; i <= positions.size(); i++) 
{
         if (i = -1) //considering it as (i==-1)
         {
               return true;
         }
         return false;
}

u have initialized i to 0 and did i++ so when will it become ==-1.
It will always return false in the first iteration itself.

Why not :

bool isEmpty() { return ( position.size == 0 ); }

Why not :

bool isEmpty() { return ( position.size == 0 ); }
//make it position.size()

yes thats the perfect solution

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.