gpt4 book ai didi

c++ - std::list 转换和复制

转载 作者:太空狗 更新时间:2023-10-29 22:57:20 26 4
gpt4 key购买 nike

我需要做以下算法:

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

然后回答 = [({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

关于c++ - std::list 转换和复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44552835/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com