Std :: list transform and copy

I need the following algorithm:

(Pseudocode)
lsi = (set_1, set_2, ..., set_n) # list of sets.
answer = [] // vector of lists of sets
For all set_i in lsi:
   if length(set_i) > 1:
        for all x in set_i:
            A = set_i.discard(x)
            B = {x}
            answer.append( (set_1, ..., set_{i-1}, A, B, set_{i+1}, ..., set_n) )

      

For example, let lsi = ({1,2}, {3}, {4,5}).

Then ansver = [({1}, {2}, {3}, {4,5}), {{2}, {1}, {3}, {4,5}), ({1, 2} , {3}, {4}, {5}), ({1,2}, {3}, {5}, {4})].

I am trying to do this (C ++) but I cannot. My code:

list<set<int>> lsi {{1, 2}, {3}, {4, 5}}; 

// Algorithm
vector<list<set<int>>> answer;
for (list<set<int>>::iterator it_lsi = lsi.begin(); it_lsi != lsi.end(); it_lsi++)
{
    for (set<int>::iterator it_si = (*it_lsi).begin(); it_si != (*it_lsi).end(); it_lsi++)
    {
        // set_i = *it_lsi, 
        // x = *it_si.
        // Creating sets A and B.
        set<int> A = *it_lsi;
        A.erase(*it_si); // A = set_i.discard(x)
        set<int> B;
        B.insert(*it_si); // B = {x}
        // Creating list which have these sets.
        list<set<int>> new_lsi;
        new_lsi.push_back(s1);
        new_lsi.push_back(s2);
        /*
        And I don't know what should I do :-(
        Object lsi must be immutable (because it connect with generator). 
        Splice transform it. Other ways I don't know.
        */
    }
}

      

could you help me? Thank.

+3


source to share


1 answer


list<set<int>> lsi {{1, 2}, {3}, {4, 5}}; 

vector<list<set<int>>> answer;
for (auto it_lsi = lsi.begin(); it_lsi != lsi.end(); ++it_lsi)
{
    if (it_lsi->size() > 1)
        for (int i : *it_lsi)
        {
            list<set<int>> res {lsi.begin(), it_lsi};
            set<int> A = *it_lsi;
            A.erase(i);
            set<int> B {i};
            res.push_back(A);
            res.push_back(B);
            res.insert(res.end(), next(it_lsi), lsi.end());
            answer.push_back(res);
        }
}   

      



Managed code with easy exit: http://ideone.com/y2x35Q

+1


source







All Articles