久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

有沒有辦法在 for 循環初始值設定項中定義兩種不

Is there a way to define variables of two different types in a for loop initializer?(有沒有辦法在 for 循環初始值設定項中定義兩種不同類型的變量?)
本文介紹了有沒有辦法在 for 循環初始值設定項中定義兩種不同類型的變量?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

您可以在 for 循環中定義 2 個相同類型的變量:

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 << endl;
  }
}

但是定義不同類型的變量是非法的:

But it is illegal to define variables of different types:

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

有沒有辦法做到這一點?(我不需要在循環內使用 i,只需使用 j.)

Is there a way to do this? (I don't need to use i inside the loop, just j.)

如果您有完全被黑和晦澀的解決方案,那對我來說沒問題.

If you have totally hacked and obscure solution, It's OK for me.

在這個人為的例子中,我知道你可以只對兩個變量使用 double.我正在尋找一個通用的答案.

In this contrived example I know you could just use double for both variables. I'm looking for a general answer.

請不要建議將任何變量移到 for 主體之外,可能對我來說不可用,因為它是一個迭代器,它必須在循環后立即消失,而 for 語句將包含在我的 foreach 中 宏:

Please do not suggest to move any of the variables outside of for body, probably not usable for me as one is an iterator that has to disappear just after the loop and the for statement is to be enclosed in my foreach macro:

#define foreach(var, iter, instr) {                  
    typeof(iter) var##IT = iter;                     
    typeof(iter)::Element var = *var##IT;            
    for (; var##_iterIT.is_still_ok(); ++var##IT, var = *var#IT) {  
      instr;                                         
    }                                                
  }

可以這樣使用:

foreach(ii, collection, {
  cout << ii;
}). 

但我需要一些像這樣使用的東西:

But I need something that will be used like that:

foreach(ii, collection)
  cout << ii;

請不要引入任何運行時開銷(但編譯可能會很慢).

Please do not introduce any runtime overhead (but it might be slow to compile).

推薦答案

這是一個使用 boost 預處理器的版本(這只是為了好玩.有關真實世界的答案,請參閱上面@kitchen 的答案):

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)) { 

}

第一部分指定了一系列聲明:(a)(b)....后面聲明的變量可以引用之前聲明的變量.第二和第三部分和往常一樣.在第二部分和第三部分出現逗號的地方,可以使用括號來防止它們分隔宏參數.

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.

我知道有兩個技巧用于聲明變量,這些變量后來在添加到宏之外的復合語句中可見.第一個使用條件,如 if:

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

然后 k 是可見的.自然地,它總是必須評估為 false.所以它不能被我們使用.另一個上下文是這樣的:

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

這就是我要在這里使用的.我們必須注意只對 COMPOUND_STATEMENT 進行一次迭代.執行增量和條件檢查的實際 for 循環必須放在最后,因此附加的復合語句屬于它.

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;
    }
}

它創建了一堆 for 語句,每個語句嵌套在另一個語句中.它擴展為:

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;
      }

這篇關于有沒有辦法在 for 循環初始值設定項中定義兩種不同類型的變量?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運算符的數學式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達式中的變量聲明)
主站蜘蛛池模板: 拍真实国产伦偷精品 | 久久国产日本 | 亚洲高清在线 | 搞黄视频免费看 | 天堂久久天堂综合色 | 久久6| 可以看黄的视频 | 国产精品九九九 | 337p日本欧洲亚洲大胆精蜜臀 | 婷婷去俺也去 | 青草青草久热精品视频在线观看 | 欧美日韩视频在线第一区 | 久久一区二区精品 | 国产伦精品一区二区三区四区视频 | 男女视频在线观看免费 | 九九九久久国产免费 | 91av亚洲 | 日韩精品一区二区三区 | 羞羞色网站 | 精品一二三 | 国产精品日韩欧美一区二区三区 | 日韩一级免费观看 | 蜜桃av鲁一鲁一鲁一鲁 | h在线免费观看 | 国产乱精品一区二区三区 | 精品在线免费观看视频 | 国产aⅴ | 亚洲九色| 久久999 | av在线播放一区二区 | 精品一区av| 国产美女自拍视频 | www久久99| 99精品久久久久久中文字幕 | 日韩综合在线 | 国产一区二区精 | 中文字幕在线免费视频 | 天天操天天舔 | 久久久久久久一区 | 国产精品自在线 | 亚洲成人蜜桃 |