The answer to that question already was given in more than thread, e. g. in last comment of http:Q_22966252.html.

You shouldn't accept solutions if your questions are not answered. It is frustrating for an expert if an already answered question comes again and there was not a single hint why the previously made comments did not sufficently answered the question.

Again, a std::set is a strange container. It is *not* a list and it is *not* an array. It isn't a dictionary either.

The differences to a list are:

- a set has unique items only, i. e. a set<int> cannot contain the item 5 twice.
- a set is ordered. The order depends on the comparision operator which defaults to the 'less' operator but can be any comparision function taking two const reference arguments of the type used as template type in set:

bool comparefunction(const T& t1, const T& t2);

Note, the compare function can be a template function but must not be a template function.

- like a list a set cannot be indexed. That is cause the elements in a set are not stored in an array (normally) but are linked (often in a binary tree) what makes it rather impossible to deduce a single item from an index beside of iterating the whole container.

The differences to a vector (array) are:

- the vector can be indexed.
- the set is always sorted, the vector can be sorted.
- the vector can have duplicate items:

vector<int> v(100, 0); // all 100 items have value 0

The differences to a map are

- the map has two template types a key type and a data type
- the set has only key type (or only data type if you like better).
- the map provides an 'indexed' access using the key for index. Differently to a 'normal' index the keys may have 'gaps' and may not be incrementable at all, e. g. a 'string' key.
- the set could have indexed access via key but that makes no sense as the 'data' to retrieve would be the key itself.
- set and map have a find function where you can retrieve an *iterator* to the item found or get the end() iterator which points beyond the container to indicate a non-existence.

All containers can be iterated using a iteration from begin() to end() and incrementing the iterator by using operator++. An iterator is a 'helper' class 'embedded' into the container class which stores 'pointers' to the current element of the container, where *current* is related to the iteration made with the iterator. So, with

set<int>::iterator iter = myset.begin();

the class object 'iter' is a copy of the iterator returned by set::begin() and *points* to the first element (node) of the set container. That always is the lowest value (related to the used comparision). By ++iter (or iter++) the iter object now points to the next item of the container. If there is no next item, iter becomes equal to the iterator returned by myset.end().

>>>> how do I access set element?
You do access a set element by using an iterator. You can retrieve an iterator by the set::find, set::begin, set::end (and set::rbegin, set::rend) and by using increment or decrement operators. Note, using the iterator returned by set::find can be a non-trivial operation though of course the data found is the same as the key input. But you can 'update' the set element using the iterator what means that the old value was erased and the new value was inserted:

>>>> You shouldn't dereference an iterator that points to s.end() ...Don't output it either.
It is a good chance that it crashes. If the set::end() iterator would have a NULL pointer to indicate the out-of-bounds, dereferencing would crash. You've been lucky that it seems to point to a real node element with an arbitrary data item.

Templates For Beginners
Or
How To Encourage The Compiler To Work For You
Introduction
This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…

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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.