[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2007-11-16
30
Medium Priority
?
878 Views
Last Modified: 2012-08-13
how do I use set?
0
Comment
Question by:Troudeloup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 5
  • 5
  • +4
30 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 400 total points
ID: 20300287
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 160 total points
ID: 20300290
#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 80 total points
ID: 20300291
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 400 total points
ID: 20300308
0
 

Author Comment

by:Troudeloup
ID: 20300530
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 480 total points
ID: 20300561
>>      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
ID: 20300640
is each item in the set called a node?
0
 

Author Comment

by:Troudeloup
ID: 20300661
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 480 total points
ID: 20300664
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 400 total points
ID: 20300667
>>is each item in the set called a node?
yes, it is
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 400 total points
ID: 20300684
>>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
ID: 20300714
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 480 total points
ID: 20300729
Elements in a set are already arranged in ascending order. What order do you need?
0
 

Author Comment

by:Troudeloup
ID: 20300735
big to small
0
 

Author Comment

by:Troudeloup
ID: 20300742
how do I display number of elements/node in the set?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 480 total points
ID: 20300766
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
ID: 20300847
     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 640 total points
ID: 20303954
>>>>   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 640 total points
ID: 20303956
>>>> 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 640 total points
ID: 20303966
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 640 total points
ID: 20303969
>>>> 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
ID: 20305517
      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 400 total points
ID: 20305542
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 240 total points
ID: 20305543
>> I don't get this part, how do I do this?

How do you do what ?
0
 

Author Comment

by:Troudeloup
ID: 20305546
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 240 total points
ID: 20305619
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
ID: 20305640
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
ID: 20305644
i have to use set.
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 240 total points
ID: 20305650
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
ID: 20306229
>>>> 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

649 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