Solved

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

Posted on 2007-11-17
4
696 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

756 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