• ### What is your GameDev Story?

This topic is 5071 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, i just want to know how to use set, from the STL. I know how vector and deque work, so, i'll pick it up quickly. Just tell me how to do to init them and how to work with them. There is an exaple i've done but i don't know how to acces value and the compiler throw me a warning message that i don't understand.Here it is: #include <iostream> #include <set> using namespace std; set<int> s; void main() { s.insert(7); s.insert(4); s.insert(9); s.insert(2); s.insert(1); } Can you just show me the most common way to work with them?

##### Share on other sites
Your example is rather limited, and it's hard to comment on a warning without knowing what it is[smile] (I see no obvious problem with your code above, as written), so:

Everything you always wanted to know about set (but were afraid to ask).

If you've got some more specific questions, then let us know.
Jim.

##### Share on other sites
Sets are always sorted, and contain only one copy of each value. You can get to the values using iterators or std::for_each.

Example with iterators:

for(set<int>::const_iterator value = s.begin(); value != s.end(); ++value) cout << *value << endl;

Edit: I can't spell, nor do I know the difference between a container and an iterator.

##### Share on other sites
Thnx, that what i was looking for...

##### Share on other sites
<random-observation>
</random-observation>

##### Share on other sites
I typically will use a set when I need to logically group some collection of items, but I don't need arbitrary access to them, but when I need them to be ordered in some way during iteration of the contents.

If I don't need ordering, then I'll just use a list.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 21
• 15
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634097
• Total Posts
3015514
×