代码之家  ›  专栏  ›  技术社区  ›  ATL_DEV

如何在C++ 11中有条件跳过for循环的后置操作?

  •  -2
  • ATL_DEV  · 技术社区  · 7 年前

    我有一个for循环,它迭代对象的向量。如果一个对象不满足某个条件,我想通过循环重复相同的对象,直到它满足该条件。

    int TrainDog(const vector<Dog> &dogs, const Cat big_cat) { 
        for (auto const dog : dogs) {
           dog->Sit();                 // tell the dog to sit
           if (!dog->IsBarking())      // if dog isn't barking
              dog->Eat(raw_burger);    //   then reward dog
           else {                      // else 
              dog->PlayWith(big_cat);  //   punish dog
              ???                      //   and train again ??? 
              big_cat++;               //   with bigger cat
           } 
        }   
    }
    

    我希望保持这个简洁的迭代器,而不是使用传统的索引变量语法。

    4 回复  |  直到 7 年前
        1
  •  2
  •   Remy Lebeau    7 年前

    我可以考虑两种选择。

    1. 使用普通 for 循环而不是范围- 对于 循环。仅当满足某些条件时才增加迭代器。

      int TrainDog(const vector<Dog> &dogs, const Cat big_cat)
      { 
         for (auto iter = dogs.begin(); iter != dogs.end(); )
         {
            auto dog = *iter;
            dog->Sit();                 // tell the dog to sit
            if (!dog->IsBarking())      // if dog isn't barking
            {          
               dog->Eat(raw_burger);    //   then reward dog
               ++iter;                  // Go on to the next dog
            }
            else
            {
                dog->PlayWith(big_cat);  //   punish dog
                big_cat++;               //   with bigger cat
                                         //   and train again. Don't increment the iterator 
            } 
         }   
      }
      
    2. 使用 while 循环内部 对于 循环直到满足某些条件。

      int TrainDog(const vector<Dog> &dogs, const Cat big_cat)
      { 
         for (auto const dog : dogs)
         {
            while ( true )
            {
               dog->Sit();                 // tell the dog to sit
               if (!dog->IsBarking())      // if dog isn't barking
               {
                  dog->Eat(raw_burger);    //   then reward dog
                  break;                   //   break out of the while loop
               }
      
               dog->PlayWith(big_cat);  //   punish dog
               big_cat++;               //   play with bigger cat
            } 
         }   
      }
      
        2
  •  8
  •   JMAA    7 年前

    在您的例子中,您有为每只狗重复一个动作的逻辑概念,所以在代码中就要这样做。也就是说,为每只狗编写一个重复动作的内部循环。

    例如:

    int TrainDog(const vector<Dog> &dogs, const Cat big_cat) { 
        for (auto const dog : dogs) {
           dog->Sit();
           while(dog->IsBarking()) {
              dog->PlayWith(big_cat);
              dog->Sit();
              big_cat++;
           } 
           dog->Eat(raw_burger);
        }   
    }
    
        3
  •  2
  •   V. Kravchenko    7 年前

    正如注释中建议的@alter,您可以在内部使用while循环。对于迭代器也有一个选项,但是解决方案要干净得多。那只会用 while 循环,使代码更加简单!另一个优点是,您不必每次都检查迭代器。

    for (auto const dog : dogs) {
       dog->Sit();
       while (dog->IsBarking())
       {
          dog->PlayWith(big_cat);
          big_cat++;
          dog->Sit();
       }
       dog->Eat(raw_burger);
    }   
    
        4
  •  1
  •   Robin Nicholson    7 年前

    要在同一对象上重复,请创建一个内部循环。

    int TrainDog(const vector<Dog> &dogs, const Cat big_cat) { 
      for (auto const dog : dogs) {
        dog->Sit();                 // tell the dog to sit
        while (dog->IsBarking()) {  // if dog is barking
          dog->PlayWith(big_cat++); //   punish dog
        }                           // and train again 
        dog->Eat(raw_burger);       // then reward dog
       } 
    }