Is there a way to define variables of two different types in a for loop initializer?

后端 未结 13 655
余生分开走
余生分开走 2020-12-19 06:12

You can define 2 variables of the same type in a for loop:

int main() {
  for (int i = 0, j = 0; i < 10; i += 1, j = 2*i) {
    cout << j << e         


        
相关标签:
13条回答
  • 2020-12-19 06:35

    Please do not suggest to move any of the variables outside of for body, probably not usable for me as the iterator has to disappear just after the loop.

    You could do this:

    #include <iostream>
    
    int main( int, char *[] ) {
        {
            float j = 0.0;
    
            for ( int i = 0; i < 10; i += 1, j = 2*i ) {
                std::cout << j << std::endl;
            }
        }
    
        float j = 2.0; // works
    
        std::cout << j << std::endl;
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-19 06:36

    Why don't you just declare and initialize your variables outside of the for loop? You can still test and increment as you have it now.

    0 讨论(0)
  • 2020-12-19 06:40
    {
      int i = 0;
      float j = 0.0;
      for ( ; i < 10; i += 1, j = 2*i) {
        cout << j << endl;
      }
    }
    

    The variables "disappear" after the block.

    0 讨论(0)
  • 2020-12-19 06:41

    This will make the iterator (or in this case, float) disappear when it's no more needed:

    int main() {
      // some code...
    
      {
        float j = 0.0;
        for (int i = 0; i < 10; i += 1, j = 2*i) {
          cout << j << endl;
        }
      }
    
      // more code...
    }
    
    0 讨论(0)
  • 2020-12-19 06:43

    EDIT: Question has changed once more. The question now explicitly wants to implement a foreach loop. The simplest answer:

    #include <boost/foreach.hpp>
    void( std::vector<int>& v ) {
       BOOST_FOREACH( int & x, v ) {
          x = x*2;
       }
    }
    

    Injecting a variable into a code block

    This is not intended as an answer, but to show a more general technique for injecting a variable into a code block. It seems as if the macro the OP is trying to define might use, even if it does incur in some overhead

    There are a couple of places where you can define a variable with different scopes. You can define a variable inside any code block, and its lifespan will be to the end of that particular block. You can define a variable in the parenthesis of a for loop and the scope will be the loop block. You can also define a variable inside an if block and its scope will be that of the if (including the else clause).

    You can combine those options above to create externally and inject variables into a code block without creating a variable whose lifespan exceeds that of the block. A practical example would be defining a foreach loop (simplified to work only on STL containers. The calling syntax would be:

    void f( std::vector<int>& container ) 
    {
       INTVECTOR_FOREACH( int & x, container )
       {
          x = x*2;
       }
    }
    

    With semantics similar to foreach in other languages: x gets referenced to each element in the container, so that the function actually doubles each value inside the integer vector.

    Now the code of the simplified macro:

    #define INTVECTOR_FOREACH( variable, container ) \
       for ( std::vector<int>::iterator it = container.begin(); it!=container.end(); ++it ) \
          if ( bool condition=false ) {} else \
             for ( variable = *it; !condition; condition=true )
    

    Generalizing the macro for any container and type requires some metaprogramming that falls out of the context of the question, but the idea of how it works (I hope) should not be too hard to follow.

    The external for iterates over the container, in each iteration we execute another for only once defining the iterating variable (int & x in the sample code). We need a condition to control the number of iterations (1) of the internal loop, and that condition is injected with an if. We choose to make the if fail so that we can ensure that the user does not get unexpected results if she writes an else after the loop... macros are tricky.

    0 讨论(0)
  • Please do not suggest to move any of the variables outside of for body, probably not usable for me as the iterator has to disappear just after the loop.

    You could still do that, and put the whole thing in curly braces to make the extra variable go out of scope.

    int main() 
    {
      {
        float j = 0.0;
        for (int i = 0; i < 10; i += 1, j = 2*i) 
        {
          cout << j << endl;
        }
      }
      // more code...
    }
    

    This way j would go out of scope right after the loop.

    0 讨论(0)
提交回复
热议问题