?
Solved

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

Posted on 2007-11-17
4
Medium Priority
?
699 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 1400 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 600 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 1400 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

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