Solved

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

Posted on 2007-11-16
30
861 Views
Last Modified: 2012-08-13
how do I use set?
0
Comment
Question by:Troudeloup
  • 11
  • 5
  • 5
  • +4
30 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 100 total points
Comment Utility
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
 
LVL 30

Assisted Solution

by:Axter
Axter earned 40 total points
Comment Utility
#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
 
LVL 43

Assisted Solution

by:ravenpl
ravenpl earned 20 total points
Comment Utility
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 100 total points
Comment Utility
0
 

Author Comment

by:Troudeloup
Comment Utility
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
 
LVL 86

Assisted Solution

by:jkr
jkr earned 120 total points
Comment Utility
>>      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
 

Author Comment

by:Troudeloup
Comment Utility
is each item in the set called a node?
0
 

Author Comment

by:Troudeloup
Comment Utility
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
 
LVL 86

Assisted Solution

by:jkr
jkr earned 120 total points
Comment Utility
No, nodes are something different, they link to each other. Entries in sets are just called 'elements'.
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 100 total points
Comment Utility
>>is each item in the set called a node?
yes, it is
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 100 total points
Comment Utility
>>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
 

Author Comment

by:Troudeloup
Comment Utility
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
 
LVL 86

Assisted Solution

by:jkr
jkr earned 120 total points
Comment Utility
Elements in a set are already arranged in ascending order. What order do you need?
0
 

Author Comment

by:Troudeloup
Comment Utility
big to small
0
 

Author Comment

by:Troudeloup
Comment Utility
how do I display number of elements/node in the set?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 86

Assisted Solution

by:jkr
jkr earned 120 total points
Comment Utility
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
 

Author Comment

by:Troudeloup
Comment Utility
     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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 160 total points
Comment Utility
>>>>   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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 160 total points
Comment Utility
>>>> what is

>>>> int, greater<int>    ?

The first is the type of the container. The second is the 'compare' function to use for ordering.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 160 total points
Comment Utility
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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 160 total points
Comment Utility
>>>> 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
 

Author Comment

by:Troudeloup
Comment Utility
      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
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 100 total points
Comment Utility
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
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 60 total points
Comment Utility
>> I don't get this part, how do I do this?

How do you do what ?
0
 

Author Comment

by:Troudeloup
Comment Utility
show an item from the set given i = n;


like,

in that case

i expect

s.(0) = 4


or something
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 60 total points
Comment Utility
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
 

Author Comment

by:Troudeloup
Comment Utility
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
 

Author Comment

by:Troudeloup
Comment Utility
i have to use set.
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 60 total points
Comment Utility
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> 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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now