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

后端 未结 13 656
余生分开走
余生分开走 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:50

    This one is also ugly, but provides also some general way for declaring multiple variables with some given name and types in a for-loop

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

    If you're having trouble with macros, there's a standard do..while trick that works perfectly:

    #define MYFOR(init, test, post, body) \
        do \
        { \
            init \
            for( ; test; post) \
                body \
        } while(0)
    

    Use it as follows:

    MYFOR( int i = 0; float j = 0.0f; , i < 10 , (i += 1, j = 2.0f * i),
        {
             cout << j << endl;
        } );
    

    It's ugly, but it does what you want: the scope of i and j is limited by the do..while loop from the macro, and it requires a semicolon at the end, so you won't get bitten by putting it in the predicate of an if/else statement.

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

    Here is a version using boost preprocessor (This is just for fun. For the real-world answer, see @kitchen's one above):

    FOR((int i = 0)(int j = 0.0), i < 10, (i += 1, j = 2 * i)) { 
    
    }
    

    The first part specifies a sequence of declarations: (a)(b).... The variables declared later can refer to variables declared before them. The second and third part are as usual. Where commas occur in the second and third parts, parentheses can be used to prevent them to separate macro arguments.

    There are two tricks known to me used to declare variables that are later visible in a compound statement added outside a macro. The first uses conditions, like an if:

    if(int k = 0) ; else COMPOUND_STATEMENT
    

    Then k is visible. Naturally, it always have to evaluate to false. So it can't be used by us. The other context is this one:

    for(int k = 0; ...; ...) COMPOUND_STATEMENT
    

    That's what i'm going to use here. We'll have to watch to only make one iteration of COMPOUND_STATEMENT. The actual for loop that does the increment and condition checking has to come at the end, so the appended compound statement appertains to it.

    #include <boost/preprocessor.hpp>
    #include <iostream>
    
    #define EMIT_DEC_(R,D,DEC) \
        for(DEC; !_k; ) 
    
    #define FOR(DECS, COND, INC) \
        if(bool _k = false) ; else \
          BOOST_PP_SEQ_FOR_EACH(EMIT_DEC_, DECS, DECS) \
            for(_k = true; COND; INC)
    
    int main() {
        FOR((int i = 0)(float j = 0.0f), i < 10, (i += 1, j = 2 * i)) {
            std::cout << j << std::endl;
        }
    }
    

    It's creating a bunch of for statements, each nested into another one. It expands into:

    if(bool _k = false) ; else
      for(int i = 0; !_k; )
        for(float j = 0.0f; !_k; )
          for(_k = true; i < 10; (i += 1, j = 2 * i)) {
            std::cout << j << std::endl;
          }
    
    0 讨论(0)
  • 2020-12-19 06:59

    With the requirements you give the simplest code I can think of is:

    for ( int i = 0; i < 10; ++i )
    {
       float f = i * 2;
       std::cout << f << std::endl;
    }
    

    You only use f as twice the value of i. The lifetime is limited to the loop and (at least in the simplified question you provide) floats are cheap to create (exactly as cheap as to assign).

    If construction of the real float (I am assuming that since i is not really an int, f may not be a float either) is much more expensive than reassigning the value, then the other solutions of encapsulating inside an extra pair of curly braces to limit scope would be the best option.

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

    Maybe I'm being dense, but why do you even have to declare the float? You just "throw it away" when you leave the loop anyways. Right?

    for(int i=0; i<10; ++i)
        cout << (float)2*i << endl;
    

    Why do you need j?

    0 讨论(0)
  • 2020-12-19 07:01

    You say the i is your own type, and you just need to generate j out of i, right? Easy. Add a member function to i's class to generate the j value, and use that always. You can probably even make a macro to "hide" the call to that member function, if you want. :-)

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