Domanda

I've created a set list in c++ and fullfilled with elements

std::set<Unit*> myUnits; 

for(std::set<Unit*>::iterator i = myUnits.begin(); i != myUnits.end(); i++) {   
    if() {}
}

So i want in if to check every element of the setlist, what have to put in if?

È stato utile?

Soluzione

Unit* pUnit = *i; will give you a pointer to a Unit object. By the way, the correct term for the container is "set", not "setlist".

Altri suggerimenti

You probably want something like:

(*i)->stringVal

I don't know exactly, what you want, but let's assume, the Unit class has a bool Unit::check() method. Then you have to write:

if (i->check()) {...}

EDIT: Sorry, I did not realize you had a set of pointers... I'm not sure that's what you actually want, because the set will compare the pointer adresses and not the Unit's content in order to define if they are equal. Here's a little code sample to show you how to use a set with Unit-objects and pointers to Unit-objects:

class Unit
{
public:
    Unit(unsigned int id, bool c)
    {
    this->id = id; // should be unique
    checked = c;
    }

    bool check() const
    {
    return checked;
    }

    unsigned int getId() const
    {
    return id;
    }

    bool operator<(const Unit &u) const // this is needed for the set<Unit>, otherwise two Units can't be compared
    {
    return this->id < u.id;
    }

private:
    bool checked;
    unsigned int id;
};

void setTest()
{
    set<Unit> myUnits;

    Unit u1(1,true);
    Unit u2(2,false);
    Unit u3(2,true);

    myUnits.insert(u1);
    myUnits.insert(u2);
    myUnits.insert(u3);

   cout << "set<Unit>:" << endl;
   for (std::set<Unit>::iterator it = myUnits.begin(); it != myUnits.end(); ++it)
   {
        if (it->check()) // you can access the Unit-object stored in the set like this...
        {
            cout << "Unit " << it->getId() << ": checked" << endl;
        }
        else
        {
                        // ... or like this
            Unit u = *it;
            cout << "Unit " << u.getId() << ": check failed" << endl;
        }
    }

    set<Unit*> myUnitPtrs;

    myUnitPtrs.insert(&u1);
    myUnitPtrs.insert(&u2);
    myUnitPtrs.insert(&u3);

    cout << "set<Unit*>:" << endl;
    for (std::set<Unit*>::iterator it = myUnitPtrs.begin(); it != myUnitPtrs.end(); ++it)
    {
        if ((*it)->check()) // you can access a Unit-Pointer like this ...
        {
            cout << "Unit " << (*it)->getId() << ": checked" << endl;
        }
        else
        {
            Unit *u = *it; // ... or like this
            cout << "Unit " << u->getId() << ": check failed" << endl;
        }
    }
}

The output should be:

set<Unit>:
Unit 1: checked
Unit 2: check failed // inserting u3 doesn't change the set as a Unit with id 2 is already present in the set
set<Unit*>:
Unit 1: checked
Unit 2: check failed
Unit 2: checked // now there's two Units with id 2, because u2 and u3 have different adresses
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top