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

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

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

問題描述

我的代碼存在以下問題:

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

返回以下錯誤:

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

而使用 double tenorData[10] 有效.

有人知道為什么嗎?

推薦答案

在 C++ 中,變長數(shù)組是不合法的.G++ 允許將其作為擴展"(因為 C 允許),因此在 G++ 中(無需 -pedantic 關于遵循 C++ 標準),您可以這樣做:

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++

如果您想要一個可變長度數(shù)組"(在 C++ 中更好地稱為動態(tài)大小的數(shù)組",因為不允許使用適當?shù)目勺冮L度數(shù)組),您要么必須自己動態(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!

或者,更好的是,使用標準容器:

Or, better yet, use a standard container:

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

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

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

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

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(liá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?(什么時候使用內(nèi)聯(lián)函數(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 仍然相關嗎?)
主站蜘蛛池模板: 日韩毛片中文字幕 | 亚洲色图综合 | 日韩中文字幕在线视频 | 亚洲天堂日韩精品 | av毛片| 国产美女在线播放 | 成人av资源在线 | 欧美一区二区成人 | 一本一道久久a久久精品综合 | 国产精品视频偷伦精品视频 | 欧美一区二区免费在线 | 亚洲在线 | jizz中国日本 | 久草视频观看 | 日韩一区二区在线视频 | 成人三级视频 | 深爱激情综合 | 欧美成人精品一区二区三区 | 秋霞电影一区二区三区 | 国产一区二区三区在线看 | 久久夜视频 | 人人性人人性碰国产 | 成人网av| 欧美综合精品 | 丝袜 亚洲 另类 欧美 综合 | 91www在线观看 | 自拍 亚洲 欧美 老师 丝袜 | 日韩在线观看 | 欧美日韩1区2区 | 亚洲高清一区二区三区 | 亚洲在线电影 | 羞羞网站免费 | 91精品国产综合久久久久久 | 在线一区二区国产 | 色在线免费视频 | 久久精品亚洲欧美日韩精品中文字幕 | 亚洲欧美激情精品一区二区 | 久久久www | 91麻豆精品国产91久久久资源速度 | 色综合久 | 日韩免费一区二区 |