Link to home
Start Free TrialLog in
Avatar of Troudeloup
Troudeloup

asked on

[noob][c++] std::set how do I use it?

how do I use set?
SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Troudeloup
Troudeloup

ASKER

I think this code is complicated enough for my purpose, in fact, I trimmed it a little bit just so that it seems simpler.


#include <iostream>
#include <set>

using namespace std;

int main()
{
      set<int, less<int> > s;
      set<int, less<int> >::iterator i;

       // what's the two line above?


       // this reads like an object instance "s" has two more numbers added to it.
      s.insert(4);
      s.insert(0);
       
      // this merely prints all, i think "i" is the index.

       cout << "The set contains the elements: " << endl;
      for (i=s.begin();  i !=s.end();  i++)
      {
      cout << *i  << endl;
      }
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
is each item in the set called a node?
this doesn't work :(


#include <iostream>
#include <set>

using namespace std;

int main()
{
      set<int> s;
      set<int> ::interator i;
      
      s.insert(4);
      s.insert(0);


      cout << "The set contains the elements: " << endl;
      for (i=s.begin();  i !=s.end();  i++)
      {
      cout << *i  << endl;
      }
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok, insert is to put a element/node into the ...set,

how do I access it?

can I arrange them?




#include <iostream>
#include <set>

using namespace std;

int main()
{
      set<int> s;
      set<int> ::interator i;
     
      s.insert(4);
      s.insert(0);


      cout << "The set contains the elements: " << endl;
      for (i=s.begin();  i !=s.end();  i++)
      {
      cout << *i  << endl;
      }
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
big to small
how do I display number of elements/node in the set?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
     set<int, greater<int> > s;
      set<int, greater<int> >::iterator i;


set < //skip  >                   //  so this calls the set template?

what is

int, greater<int>    ?



and i am guessing that

in

      set<int, greater<int> > s;
      set<int, greater<int> >::iterator i;



s;          and
::iterator i;

are put in two lines following the big chunk because there can be more settings to the set<> right?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
      cout << "The set contains the elements: " << endl;
      for (i=s.begin();  i !=s.end();  i++)
      {
      cout << *i  << endl;
      }



I don't get this part, how do I do this?

i = 5;
cout << s.i << endl;


SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
show an item from the set given i = n;


like,

in that case

i expect

s.(0) = 4


or something
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
well, i ll admit (yet again)
that I am doing homework,
but i am trying to figure out the bits myself.




I think these two terms will help:

I am trying to learn the syntax of

its accessors and mutators.
i have to use set.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>>> show an item from the set given i = n;

      set<int, greater<int> > s;
      set<int, greater<int> >::iterator iter;

      int n = 4;
      int i = 0;
      for (iter=s.begin(), i = 0;  iter !=s.end();  ++iter, ++i)
      {
            if (i == n)
           {
                cout << *iter  << endl;
                break;
           }
      }

You see a set is not very suitable for 'indexed' access. The purpose of a set is to get 'unique' items into an automatically sorted container:

     set<int> s;
     s.insert(5);
     s.insert(4);
     s.insert(1);

     set<int>::iterator iter = s.begin();
     cout << *iter;  // shows 1

     *iter = 6;    // update first element

      iter = s.begin();
      cout << *iter;  // shows 4

Note, the set::iterator::operator*() is an overloaded derefencere operator. It isn't a dereference of a 'pointer' as set::iterator is a class type and not a pointer. But of course it is 'willingly' made that you can use an iterator like a pointer.