如何确保,我已经使用插入操作std :: set in loop中的每对元素?

问题描述 投票:0回答:2
std::set<BooleanFunction>::iterator  it;
for(it=closure.begin (); it!=closure.end (); ++it)
{ 

  std::set<BooleanFunction>::iterator  jit;
  for(jit=closure.begin (); jit!=closure.end (); ++jit)
  {

    for(uint i=0U; i<it->Arity (); ++i)
    { BooleanFunction  f = it->Superposition (*jit, i);

      if( f.Arity () <= test.Arity () )
       closure.insert (f);
    }
  }
}

我如何确保每两个元素彼此具有叠加?

编辑:自我叠加也是可能的:f(f(x)),f(f(f(x))),... - 是的。

c++ stl iterator set
2个回答
2
投票

我认为你可以使用2套如下。

std::set<BooleanFunction> builder
std::set<BooleanFunction>::iterator  it;
for(it=closure.begin (); it!=closure.end (); ++it)
{ 
    bulider.insert(*it);
    for(uint i=0U; i<it->Arity (); ++i)
    {
        BooleanFunction  f = it->Superposition (*it, i);
        if( f.Arity () <= test.Arity () )
            builder.insert (f);
    }
}

0
投票

额外的循环体操看起来非常可怕:

void  smart_insert (std::set<BooleanFunction>  &closure,
                    std::set<BooleanFunction>::iterator  it,
                    std::set<BooleanFunction>::iterator jit,
                       const BooleanFunction   &f, uint arity)
{
  if( f.Arity () <= arity && closure.find (f) == closure.end () )
  { 
    if( f < *jit )
    { std::set<BooleanFunction>::iterator  nit;
      ++it;

      for(nit=closure.begin (); nit!=it; ++nit)
       for(uint i=0U; i<nit->Arity (); ++i)
       { BooleanFunction  ff = nit->Superposition (f, i);

         smart_insert (closure, nit, closure.find (f), ff, arity);
       } // end for i
    } // end if *jit
    if( f < *it )
    { std::set<BooleanFunction>::iterator  njit;
      ++jit;

      for(njit=closure.begin (); njit!=jit; ++njit)
       for(uint i=0U; i<f.Arity (); ++i)
       { BooleanFunction  ff = f.Superposition (*njit, i);

         smart_insert (closure, closure.find (f), njit, ff, arity);
       } // end for i
    } // end if *it
    closure.insert (f);
  } // end if arity & unique
}

// ...
std::set<BooleanFunction>::iterator  it;
for(it=closure.begin (); it!=closure.end (); ++it)
{ 

  std::set<BooleanFunction>::iterator  jit;
  for(jit=closure.begin (); jit!=closure.end (); ++jit)
  {

    for(uint i=0U; i<it->Arity (); ++i)
    { BooleanFunction  f = it->Superposition (*jit, i);

      smart_insert (closure, it, jit, f, test.Arity () );
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.