Solved

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

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now