[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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…

650 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