1
votes
0000 0000
0001 1000
0010 0001
0011 1100
0100 0010
0101 0101
0110 1001
0111 1110
1000 0100
1001 0110
1010 1010
1011 0111
1100 0011
1101 1011
1110 1101
1111 1111

I have two vectors a and b of type std::vector<std::bitset<4> >

vector a --> 1st column vector b --> 2nd column

I want to form cycles using these 2 vectors

a[0] == b[0] == 0000 -- > no need to form a cycle

a[1] != b[1] --> cycle can be formed.

cycle 'c' is formed as follows: c = {a[1]==0001,b[1]==1000,...

then search for b[1] =1000 in `vector a` and add the corresponding value in b at that position

like now b[1] =1000 is at a[8], hence b[8] = 0100 is added to the cycle c = {a[1]=0001,b[1]=1000,b[8]=0100,...}

now b[8] = 0100 is at a[4], hence b[4] = 0010 is added to the cycle c= {a[1]=0001,b[1]=1000,b[8]=0100,b[4]=0010,..}

now b[4] =0010 is at a[2], hence b[2] == 0001 but b[2] = 0001 == 1st element in cycle 'c' hence leave it and this completes the cycle.

The below code is where I create a vector v1 to form the cycle 'c' , push values into it, perform operations and return the vector or cycle c.

x=a.at(i), y=b.at(i)

example : x=a.at(1),y=b.at(1)

std::vector<std::bitset<4> > getvector(int x, int y,const std::vector<std::bitset<4> >& a,const std::vector<std::bitset<4> >& b ) {
std::vector<std::bitset<4>> resultvector;
    resultvector.push_back(x);//push a.at(1) into r 
    resultvector.push_back(y);//push b.at(1) into r 
    while(resultvector.back()!=resultvector.front()) //run the loop until last element in vector r != first element in r and comes out when == 
    {

        //loop begin
        int pos = find(a,y);
        r.push_back(b.at(pos));
        //loop end 
    }
    return resultvector;
}

I want to know how to write this loop to perform the above operation.

1
can anyone help me with how to write this loopuser3898956
I have given an example for my explanation.It would be clear nowuser3898956
may be you should start by explaining the original problem and then explaining you vector solutionarash kordi
the formation of cycle using the two vector is my problemuser3898956
can any one help me with how to write loop to form this cycleuser3898956

1 Answers

0
votes

r is empty to begin with, therefore front/back may not be called. Your code and explanations make no sense.

Try to explain what you want to do without code, then think about loops and invariants.

Also, give your function an appropriate name. getVector is too general, and does not indicate your intent. The resultant vector is a function of the input, and the name must indicated in which way. Then we would be better able to help you.