5

Possible Duplicate:
How to do the vector of sets in C++?

I want to have a set for the different levels that are in my code. A set at each level will be holding integer values. The number of these sets should be dynamic depending on the number of levels required ( which is given as input ).

For this, I wanted to have a dynamic set structure. How can I achieve this? Can I go for a vector with as many pointers to the sets as required? How do I achieve this? Is there any other method.

Can somebody give me a snippet for it?

Community
  • 1
  • 1
Aakash Anuj
  • 55
  • 1
  • 1
  • 6

2 Answers2

9
vector<set<int> > my_sets;

Adding an element to i-th set:

int number;
//...
my_sets[i].insert(number);

Searching an element in i-th set:

if(my_sets[i].find(number) != my_sets[i].end())
{
      // Number found
}

Iterate over i-th set:

for(set<int> :: iterator it = my_sets[i].begin(); it != my_sets[i].end();++it)
{
   // do something with integer value *it
}

Add a new set:

set<int> temp;
temp.insert(a);temp.insert(b);//...
my_sets.push_back(temp);

Erase i-th set:

my_sets.erase(my_sets.begin() + i );
dpetek
  • 635
  • 1
  • 5
  • 11
2

A vector of sets is simply std::vector<std::set<type>>. Are you looking for something else?