Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::set for each element in an array

I need a functionality of std::set for each element in an array. How can I achieve this functionality?

I started with allocating dynamic array of std set in C++ as follows:

set<int>* entry;

followed by allocation:

entry = (set<int>*)malloc(sizeof(set<int>)*32);

No compilation problem, but the runtime fails with segmentation fault when any element is accessed:

entry[0].insert(23);

Any help is highly appreciated.

like image 231
lashgar Avatar asked Nov 28 '22 16:11

lashgar


1 Answers

What about

#include <set>
#include <vector>

int main()
{
        std::vector < std::set<int> > entry(32); // std::vector constructor makes 32 calls to std::set<int> constructor
        entry[0].insert(23);
        // std::vector destructor makes 32 calls to std::set<int> destructor
}
like image 164
TemplateRex Avatar answered Dec 01 '22 05:12

TemplateRex