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

傳遞給非主函數(shù)的數(shù)組上基于范圍的 for 循環(huán)

Range based for-loop on array passed to non-main function(傳遞給非主函數(shù)的數(shù)組上基于范圍的 for 循環(huán))
本文介紹了傳遞給非主函數(shù)的數(shù)組上基于范圍的 for 循環(huán)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

當(dāng)我嘗試在 gcc 4.8.2 中編譯以下代碼時,出現(xiàn)以下錯誤:

<塊引用>

test.cc: 在函數(shù)‘void foo(int*)’中:test.cc:15:16: 錯誤:沒有匹配的函數(shù)調(diào)用‘begin(int*&)’for (int i : bar) {^

以及來自模板庫深處的其他一些人.

#include 使用命名空間標(biāo)準(zhǔn);void foo(int*);int main() {int bar[3] = {1,2,3};for (int i : bar) {cout<<我<<結(jié)束;}富(酒吧);}void foo(int* bar) {for (int i : bar) {cout<<我<<結(jié)束;}}

如果我重新定義 foo 以使用索引的 for 循環(huán),那么代碼會編譯并按預(yù)期運行.此外,如果我將基于范圍的輸出循環(huán)移動到 main 中,我也會得到預(yù)期的行為.

如何將數(shù)組 bar 傳遞給 foo 使其能夠在其上執(zhí)行基于范圍的 for 循環(huán)?

解決方案

隨著數(shù)組衰減變成一個指針,你'丟失了一條重要信息:它的大小.

使用數(shù)組引用,您的基于范圍的循環(huán)可以工作:

void foo(int (&bar)[3]);int main() {int bar[3] = {1,2,3};for (int i : bar) {cout<<我<<結(jié)束;}富(酒吧);}void foo(int (&bar)[3]) {for (int i : bar) {cout<<我<<結(jié)束;}}

或者,以通用方式(即在函數(shù)簽名中不指定數(shù)組大小),

模板void foo(int (&bar)[array_size]) {for (int i : bar) {cout<<我<<結(jié)束;}}

試試

When I try to compile the following code in gcc 4.8.2, I get the following error:

test.cc: In function ‘void foo(int*)’:
test.cc:15:16: error: no matching function for call to ‘begin(int*&)’
   for (int i : bar) {
                ^

Along with a bunch of others from deeper in the template library.

#include <iostream>
using namespace std;

void foo(int*);

int main() {
  int bar[3] = {1,2,3};
  for (int i : bar) {
    cout << i << endl;
  }
  foo(bar);
}

void foo(int* bar) {
  for (int i : bar) {
    cout << i << endl;
  }
}

If I redefine foo to use an indexed for loop, then the code compiles and behaves as expected. Also, if I move the range-based output loop into main, I get the expected behaviour as well.

How do I pass the array bar to foo in such a way that it is capable of executing a range-based for-loop on it?

解決方案

With the array decaying into a pointer you're losing one important piece of information: its size.

With an array reference your range based loop works:

void foo(int (&bar)[3]);

int main() {
  int bar[3] = {1,2,3};
  for (int i : bar) {
    cout << i << endl;
  }
  foo(bar);
}

void foo(int (&bar)[3]) {
  for (int i : bar) {
    cout << i << endl;
  }
}

or, in a generic fashion (i.e. without specifying the array size in the function signature),

template <std::size_t array_size>
void foo(int (&bar)[array_size]) {
  for (int i : bar) {
    cout << i << endl;
  }
}

Try it out

這篇關(guān)于傳遞給非主函數(shù)的數(shù)組上基于范圍的 for 循環(huán)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

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;(比較運算符的數(shù)學(xué)式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區(qū)別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達(dá)式中的變量聲明)
主站蜘蛛池模板: 五月综合激情网 | 欧美毛片免费观看 | 欧美αv | 国产免费自拍 | 国产精品片aa在线观看 | av在线免费不卡 | 香蕉av免费 | 国产激情视频网址 | 日本中文字幕一区 | 99精品在线免费观看 | 色综合久久久久 | 久久久久久久网 | 成人精品一区二区三区中文字幕 | 久久久爽爽爽美女图片 | 久久久免费精品 | 天天草狠狠干 | 欧美日韩国产精品一区二区 | 日韩欧美在 | 亚洲精品一区二区二区 | 成人午夜网站 | 一区二区三区中文字幕 | 国产精品久久久久久妇女 | 少妇一级淫片免费播放 | 欧美视频二区 | 国产一区二区 | 国产一区二区自拍 | 91在线免费视频 | 秋霞在线一区二区 | 久久久久久久亚洲精品 | 超碰91在线| 人人操日日干 | 免费的一级视频 | 欧洲国产精品视频 | 看片天堂| 一区二区在线不卡 | 日韩精品中文字幕在线 | 亚洲精品一 | 久热久草 | 一级在线免费观看 | 99精品一区二区三区 | 中文字幕一区二区三区乱码在线 |