Solved

[noob][c++] set:: accessors & mutators

Posted on 2007-11-17
4
698 Views
Last Modified: 2012-06-21
#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(1);
      s.insert(2);
      s.insert(4);
      s.insert(3);
      s.insert(6);
         

       cout << "The set contains the elements: " << endl;
       for (i=s.begin();  i !=s.end();  i++)
       {
       cout << *i  << endl;
       }
}



http://www.cplusplus.com/reference/stl/set/



       cout << "The set contains the elements: " << endl;
       for (i=s.begin();  i !=s.end();  i++)
       {
       cout << *i  << endl;
       }



while I understand that this is a for loop, I don't get how the individual parts work.


1)  how do I cout an element of the set?

     i know it's not

     cout << s << endl;


2)  more to follow.
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
  • 2
4 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 350 total points
ID: 20305722
you have an iterator object called i.
you have access to the first element in set, by doing:
i = s.begin;     // s is the set
now i points to the first element in set

to get element content, you have to use *i, this way:

int someelem = *i;
0
 

Author Comment

by:Troudeloup
ID: 20305725
also, I cannot

cout << *i << endl;

directly right?
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 150 total points
ID: 20305736
>> 1)  how do I cout an element of the set?

Euhm ... Just look at the code :

       cout << *i  << endl;

i is an iterator that points to an element in the set. You dereference the iterator to get the value using *i.
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 350 total points
ID: 20305737
yes, you can, but as far as you have initialized the iterator with:
 i = s.begin();
or other similar
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

705 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