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
我有两个类型std::vector<std::bitset<4> >
vector a --> 1st column
vector b --> 2nd column
我想用这两个载体形成周期
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.
下面的代码是我创建向量v1以形成循环'c',将值推入其中,执行操作并返回向量或循环c的地方。
x = a.at(i),y = b.at(i)
示例: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;
}
我想知道如何编写此循环来执行上述操作。
答案 0 :(得分:0)
r开头为空,因此可能无法调用前/后。你的代码和解释毫无意义。
尝试解释在没有代码的情况下你想做什么,然后考虑循环和不变量。
另外,请为您的函数指定一个合适的名称。 getVector
过于笼统,并不表示您的意图。结果向量是输入的函数,名称必须以哪种方式表示。然后我们会更好地帮助你。