Implement a function
void sign_join(vector< list<int> > &VL,list<int> &L)
that, given a vector of lists VL
generate a L list where they are
First concatenated all the first non-negative subsequence of VL [0], VL [1] , ...
After the negatives.
Then again the non-negatives.
So continuing until all the VLs are finished
Examples: VL: [[1,2, -1, -2,3,4, -3, -4], [5,6, -5, -6,7,8, -7, -8], [- 9 , -10]],
= > L: [1,2,5,6, -1, -2, -5, -6, -9, -10,3,4,7,8, -3, -4, -7, -8],
The code I have is the following:
int sign(int &x){
if(x>=0){
return 1;
}else{
return 0;
}
}
bool vectorempty(vector<list<int>> &VL) {
for(int i=0; i<VL.size(); i++) {
if(!VL[i].empty()) {
return false;
}
}
return true;
}
int main(int argc, char *argv[]) {
vector<list<int>> vl = {{1,2,-3}, {3,4,-5,-1}, {5,6,-8}};
list<int> l;
int s = 1;
int i=0;
while(vectorempty(vl)==false){
auto it = vl[i].begin();
while(it != vl[i].end() && sign(*it)==s) {
l.push_back(*it);
it=vl[i].erase(it);
}
if(i == (vl.size()-1)) {
i=0;
s=-s;
}else{
++i;
}
}
for(int &x:l) cout<<x<<" ";
return 0;
}
But it remains in an infinite loop and I do not know how to solve it.