0

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?

hivert
  • 10,579
  • 3
  • 31
  • 56
snake plissken
  • 2,649
  • 10
  • 43
  • 64

3 Answers3

3

You probably want something like:

(*i)->stringVal
drdwilcox
  • 3,833
  • 17
  • 19
2

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".

Dabbler
  • 9,733
  • 5
  • 41
  • 64
  • yeah i confused it from the setlist in java. For example how i ve access to the first element of the myUnits?? – snake plissken Nov 21 '11 at 17:07
  • If you're asking about the order of the elements, they are (by default) sorted according to their `<` operator (you can define your own version of that operator if you like). – Dabbler Nov 21 '11 at 17:09
1

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
Ben
  • 4,486
  • 6
  • 33
  • 48