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

Array[n] vs Array[10] - 用變量 vs 實(shí)數(shù)初始化數(shù)組

Array[n] vs Array[10] - Initializing array with variable vs real number(Array[n] vs Array[10] - 用變量 vs 實(shí)數(shù)初始化數(shù)組)
本文介紹了Array[n] vs Array[10] - 用變量 vs 實(shí)數(shù)初始化數(shù)組的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我的代碼存在以下問(wèn)題:

I am having the following issue with my code:

int n = 10;
double tenorData[n]   =   {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

返回以下錯(cuò)誤:

error: variable-sized object 'tenorData' may not be initialized

而使用 double tenorData[10] 有效.

有人知道為什么嗎?

推薦答案

在 C++ 中,變長(zhǎng)數(shù)組是不合法的.G++ 允許將其作為擴(kuò)展"(因?yàn)?C 允許),因此在 G++ 中(無(wú)需 -pedantic 關(guān)于遵循 C++ 標(biāo)準(zhǔn)),您可以這樣做:

In C++, variable length arrays are not legal. G++ allows this as an "extension" (because C allows it), so in G++ (without being -pedantic about following the C++ standard), you can do:

int n = 10;
double a[n]; // Legal in g++ (with extensions), illegal in proper C++

如果您想要一個(gè)可變長(zhǎng)度數(shù)組"(在 C++ 中更好地稱為動(dòng)態(tài)大小的數(shù)組",因?yàn)椴辉试S使用適當(dāng)?shù)目勺冮L(zhǎng)度數(shù)組),您要么必須自己動(dòng)態(tài)分配內(nèi)存:

If you want a "variable length array" (better called a "dynamically sized array" in C++, since proper variable length arrays aren't allowed), you either have to dynamically allocate memory yourself:

int n = 10;
double* a = new double[n]; // Don't forget to delete [] a; when you're done!

或者,更好的是,使用標(biāo)準(zhǔn)容器:

Or, better yet, use a standard container:

int n = 10;
std::vector<double> a(n); // Don't forget to #include <vector>

如果你仍然想要一個(gè)合適的數(shù)組,你可以在創(chuàng)建它時(shí)使用常量,而不是變量:

If you still want a proper array, you can use a constant, not a variable, when creating it:

const int n = 10;
double a[n]; // now valid, since n isn't a variable (it's a compile time constant)

同樣,如果你想從 C++11 中的函數(shù)中獲取大小,你可以使用 constexpr:

Similarly, if you want to get the size from a function in C++11, you can use a constexpr:

constexpr int n()
{
    return 10;
}

double a[n()]; // n() is a compile time constant expression

這篇關(guān)于Array[n] vs Array[10] - 用變量 vs 實(shí)數(shù)初始化數(shù)組的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉(zhuǎn)換為 HSV 并將 HSV 轉(zhuǎn)換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類型變量轉(zhuǎn)換為字符串?)
When to use inline function and when not to use it?(什么時(shí)候使用內(nèi)聯(lián)函數(shù),什么時(shí)候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相關(guān)嗎?)
主站蜘蛛池模板: 人人插人人爱 | 免费观看的黄色网址 | 在线观看欧美日韩 | 天天综合天天 | 午夜999| 色婷婷精品国产一区二区三区 | 色综合视频在线观看 | 一级黄色网 | 欧美mv日韩mv国产 | 成人免费观看网站 | 超碰人人人人 | 国产黄色免费 | 久久视频免费在线观看 | 91精品成人| 三级福利视频 | 午夜精品久久久久久久久久久久 | 一区二区三区国产精品 | 国产成人97精品免费看片 | 精品黄色片 | 国产高清视频在线播放 | 国产99热| 亚洲免费久久 | 国产探花视频在线观看 | 中文字幕理伦片免费看 | 一级免费av | 日韩毛片视频 | 亚洲丝袜av | 免费三级网站 | 一区二区三区在线播放 | 黄频在线观看 | av2014天堂网| 日日操夜夜干 | 狠狠操夜夜操 | 99热99re6国产在线播放 | 欧美日韩亚洲一区二区 | 九九热视频在线 | 久艹在线观看 | 18成人免费观看网站 | 丝袜美腿亚洲综合 | 午夜伦理视频 | 欧美视频在线观看一区 |