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

how do I use set?
TroudeloupAsked:
Who is Participating?
 
itsmeandnobodyelseConnect With a Mentor Commented:
The default for the compare function is the built-in operator< of a type. In case of an int it means 'ascending' order.

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

is equivalent to

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

Here the template function less<T> was used which was implemented like

template <class T>
bool less(const T& t1, const T& t2)
{
       return t1 < t2;
}

The 'instantiation of the 'less' function pointer will fail if the type doesn't provide operator< function, e. g.

class Rectangle
{
      int l, t, r, b;
};

set<Rectangle, less<Rectangle> >;  // error: no operator<

0
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
set is a templated collection class, it is mostly like a list, but will allow you to store just unique values.
Have a look to this tutorial:
http://www.pottsoft.com/home/stl/stl.htmlx#Set_and_Multiset
0
 
AxterConnect With a Mentor Commented:
#include <iostream>
#include <set>

using namespace std;

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

    s.insert(4);
    s.insert(0);
    s.insert(-9);
    s.insert(7);
    s.insert(-2);
    s.insert(4);
    s.insert(2);

    cout << "The set contains the elements: ";
    for (i=s.begin(); i!=s.end(); i++) cout << *i << ' ';
    cout << endl;
}
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
0
 
TroudeloupAuthor Commented:
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;
      }
}
0
 
jkrConnect With a Mentor Commented:
>>      set<int, less<int> > s;
>>      set<int, less<int> >::iterator i;
>>       // what's the two line above?

These two lines declare a set of integers and an iterator to access them. An iterator is similar to a pointer to an element (but not equal to a pointer)

BTW, for simple types such as 'int', you can also write

      set<int> s;
      set<int>::iterator i;
0
 
TroudeloupAuthor Commented:
is each item in the set called a node?
0
 
TroudeloupAuthor Commented:
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;
      }
}
0
 
jkrConnect With a Mentor Commented:
No, nodes are something different, they link to each other. Entries in sets are just called 'elements'.
0
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
>>is each item in the set called a node?
yes, it is
0
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
>>Entries in sets are just called 'elements'.
That depends on book's author. When talking about the subyacent implementation of set, author name them as nodes.
0
 
TroudeloupAuthor Commented:
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;
      }
}
0
 
jkrConnect With a Mentor Commented:
Elements in a set are already arranged in ascending order. What order do you need?
0
 
TroudeloupAuthor Commented:
big to small
0
 
TroudeloupAuthor Commented:
how do I display number of elements/node in the set?
0
 
jkrConnect With a Mentor Commented:
Well, then you could use

#include <iostream>
#include <set>

using namespace std;

int main()
{
      set<int, greater<int> > s;
      set<int, greater<int> >::iterator 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;
      }
}

>>how do I display number of elements/node in the set?

just call

cout << "Size: " << s.size();
0
 
TroudeloupAuthor Commented:
     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?
0
 
itsmeandnobodyelseConnect With a Mentor Commented:
>>>>   Entries in sets are just called 'elements'.
That is true. 'Node' is an internal item of the container which holds the 'element' or 'item' but 'is not' an 'element' or 'item'. Moreover, some containers were not organized using nodes which - as jkr mentioned - were linked to each other.

Regards, Alex

0
 
itsmeandnobodyelseConnect With a Mentor Commented:
>>>> what is

>>>> int, greater<int>    ?

The first is the type of the container. The second is the 'compare' function to use for ordering.
0
 
itsmeandnobodyelseConnect With a Mentor Commented:
>>>> set<Rectangle, less<Rectangle> >;  // error: no operator<

You can 'heal' that by

class Rectangle
{
      int l, t, r, b;
public:
      ...
      bool operator<(const Rectangle& rect) const
      {
             return l < rect.l ||
                (l == rect.l && t < rect.t)  ||
                (l == rect.l && t == rect.t && r < rect.r)  ||
                (l == rect.l && t == rect.t && r < rect.r && b < rect.b));
      }
};
0
 
TroudeloupAuthor Commented:
      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;


0
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
you cannot do this directly, because it is not a vector-like collection. you have to hop from the 1st element to the 5th with a for() loop of similar.
0
 
Infinity08Connect With a Mentor Commented:
>> I don't get this part, how do I do this?

How do you do what ?
0
 
TroudeloupAuthor Commented:
show an item from the set given i = n;


like,

in that case

i expect

s.(0) = 4


or something
0
 
Infinity08Connect With a Mentor Commented:
A set is not a normal sequence container like a vector eg. It is an associative container where the values are also the keys.

If what you want to do is access the set as if it were a sequence container, then you probably want to use a different kind of container.

What is it that you want to store, and how do you want access to that data ?

        http://www.cplusplus.com/reference/stl/set/
0
 
TroudeloupAuthor Commented:
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.
0
 
TroudeloupAuthor Commented:
i have to use set.
0
 
Infinity08Connect With a Mentor Commented:
They're all listed in the link I posted in my previous post. Just click on the one you want more info about, and you'll find what you need including code examples.
0
 
itsmeandnobodyelseCommented:
>>>> 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.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.