std :: list转换和复制

顺其自然

我需要制定以下算法:

(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) )

例如,令lsi =({1,2},{3},{4,5})。

然后ansver = [({1},{2},{3},{4,5}),({2},{1},{3},{4,5}),({1,2} ,{3},{4},{5}),({1,2},{3},{5},{4})]。

我正在尝试(C ++),但是我不能。我的代码:

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.
        */
    }
}

你可以帮帮我吗?谢谢。

戴尔
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);
        }
}   

具有简单输出的可运行代码:http//ideone.com/y2x35Q

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章