Jump to content
  • Advertisement
Sign in to follow this  
Slug

question about set

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!