<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # C++ 數組 > 原文: [https://www.programiz.com/cpp-programming/arrays](https://www.programiz.com/cpp-programming/arrays) #### 在本教程中,我們將學習如何使用數組。 我們將借助示例學習在 C++ 編程中聲明,初始化和訪問數組元素。 在 C++ 中,數組是一個變量,可以存儲相同類型的多個值。 例如, 假設一班有 27 個學生,我們需要存儲所有學生的成績。 無需創建 27 個單獨的變量,我們只需創建一個數組即可: ```cpp double grade[27]; ``` 此處,`grade`是最多可容納 27 個`double`類型元素的數組。 在 C++ 中,聲明數組后不能更改數組的大小和類型。 * * * ## C++ 數組聲明 ```cpp dataType arrayName[arraySize]; ``` 例如, ```cpp int x[6]; ``` 這里, * `int` - 要存儲的元素類型 * `x` - 數組名稱 * `6` - 數組的大小 * * * ## C++ 數組中的訪問元素 在 C++ 中,數組中的每個元素都與一個數字關聯。 該數字稱為數組索引。 我們可以使用這些索引訪問數組的元素。 ```cpp // syntax to access array elements array[index]; ``` 考慮上面我們看到的數組`x`。 ![C++ Array Declaration](https://img.kancloud.cn/e4/2c/e42c9c94c3862da80dfd59eefc885301_1194x312.png "Elements of an array in C++") C++ 數組元素 ### 幾件事要記住: * 數組索引以`0`開頭。 含義`x [0]`是存儲在索引`0`的第一個元素。 * 如果數組的大小為`n`,則最后一個元素存儲在索引`(n-1)`處。 在此示例中,`x [5]`是最后一個元素。 * 數組的元素具有連續的地址。 例如,假設`x[0]`的起始地址為`2120d`。 然后,下一個元素`x[1]`的地址將為`2124d`,`x[2]`的地址將為`2128d`,依此類推。 這里,每個元素的大小增加 4。這是因為`int`的大小是 4 個字節。 * * * ## C++ 數組初始化 在 C++ 中,可以在聲明期間初始化數組。 例如, ```cpp // declare and initialize and array int x[6] = {19, 10, 8, 17, 9, 15}; ``` ![C++ Array Initialization](https://img.kancloud.cn/23/48/2348f17e24593bf7d388659048ef4906_1194x312.png "C++ Array elements and their data") C++ 數組元素及其數據 聲明期間初始化數組的另一種方法: ```cpp // declare and initialize an array int x[] = {19, 10, 8, 17, 9, 15}; ``` 在這里,我們沒有提到數組的大小。 在這種情況下,編譯器會自動計算大小。 * * * ### 空成員的 C++ 數組 在 C++ 中,如果數組的大小為`n`,我們最多可以在數組中存儲`n`個元素。 但是,如果我們存儲的元質數少于`n`個,將會發生什么。 For example, ```cpp // store only 3 elements in the array int x[6] = {19, 10, 8}; ``` 在此,數組`×`的大小為`6`。 但是,我們僅用 3 個元素對其進行了初始化。 在這種情況下,編譯器會為其余位置分配隨機值。 通常,此隨機值就是`0`。 ![C++ Array with empty members](https://img.kancloud.cn/4f/5c/4f5ce2404f734fd073c1220e3d2a86e1_1194x416.png "C++ Array with empty members") 空數組成員將自動分配值 0 * * * ## 如何插入和打印數組元素? ```cpp int mark[5] = {19, 10, 8, 17, 9} // change 4th element to 9 mark[3] = 9; // take input from the user // store the value at third position cin >> mark[2]; // take input from the user // insert at ith position cin >> mark[i-1]; // print first element of the array cout << mark[0]; // print ith element of the array cout >> mark[i-1]; ``` * * * ## 示例 1:顯示數組元素 ```cpp #include <iostream> using namespace std; int main() { int numbers[5] = {7, 5, 6, 12, 35}; cout << "The numbers are: "; // Printing array elements // using range based for loop for (const int &n : numbers) { cout << n << " "; } cout << "\nThe numbers are: "; // Printing array elements // using traditional for loop for (int i = 0; i < 5; ++i) { cout << numbers[i] << " "; } return 0; } ``` **輸出** ```cpp The numbers are: 7 5 6 12 35 The numbers are: 7 5 6 12 35 ``` 在這里,我們使用了`for`循環從`i = 0`迭代到`i = 4`。 在每次迭代中,我們都打印了`numbers[i]`。 我們再次使用基于范圍的`for`循環來打印出數組的元素。 要了解有關此循環的更多信息,請檢查[基于 C++ 的循環范圍](/cpp-programming/ranged-for)。 **注意**:在基于范圍的循環中,我們使用代碼`const int &n`而不是`int n`作為范圍聲明。 但是,`const int &n`更可取,因為: 1. 使用`int n`只需在每次迭代期間將數組元素復制到變量`n`。 這不是高效的內存。 但是, `& n`使用數組元素的內存地址來訪問其數據,而無需將其復制到新變量中。 這樣可以節省內存。 2. 我們只是在打印數組元素,而不修改它們。 因此,我們使用`const`以免意外更改數組的值。 * * * ## 示例 2:從用戶那里獲取輸入并將其存儲在數組中 ```cpp #include <iostream> using namespace std; int main() { int numbers[5]; cout << "Enter 5 numbers: " << endl; // store input from user to array for (int i = 0; i < 5; ++i) { cin >> numbers[i]; } cout << "The numbers are: "; // print array elements for (int n = 0; n < 5; ++n) { cout << numbers[n] << " "; } return 0; } ``` **輸出** ```cpp Enter 5 numbers: 11 12 13 14 15 The numbers are: 11 12 13 14 15 ``` 再次,我們使用了`for`循環從`i = 0`迭代到`i = 4`。 在每次迭代中,我們從用戶那里獲取輸入并將其存儲在`numbers[i]`中。 然后,我們使用了另一個`for`循環來打印所有數組元素。 * * * ## 示例 3:使用`for`循環顯示數組元素的總和和平均值 ```cpp #include <iostream> using namespace std; int main() { // initialize an array without specifying size double numbers[] = {7, 5, 6, 12, 35, 27}; double sum = 0; double count = 0; double average; cout << "The numbers are: "; // print array elements // use of range-based for loop for (const double &n : numbers) { cout << n << " "; // calculate the sum sum += n; // count the no. of array elements ++count; } // print the sum cout << "\nTheir Sum = " << sum << endl; // find the average average = sum / count; cout << "Their Average = " << average << endl; return 0; } ``` **輸出** ```cpp The numbers are: 7 5 6 12 35 27 Their Sum = 92 Their Average = 15.3333 ``` 在此程序中: 1. 我們已初始化名為`num`的`double`數組,但未指定其大小。 我們還聲明了三個雙變量`sum`,`count`和`average`。 這里是`sum =0`和`count = 0`。 2. 然后,我們使用基于范圍的`for`循環來打印數組元素。 在循環的每次迭代中,我們將當前數組元素添加到`sum`中。 3. 在每次迭代中,我們還將`count`的值增加`1`,以便可以在`for`循環結束時獲得數組的大小。 4. 在打印所有元素之后,我們將打印所有數字的總和和平均值。 數字的平均值由`average = sum / count;`給出 **注意**:我們使用基于范圍的`for`循環而不是常規的`for`循環,因為我們不知道數組的大小。 普通的`for`循環要求我們指定迭代次數,該次數由數組的大小指定。 但是基于范圍的`for`循環不需要這樣的規范。 * * * ## C++ 數組越界 如果我們聲明一個大小為 10 的數組,則該數組將包含索引從 0 到 9 的元素。 但是,如果嘗試訪問索引為 10 或大于 10 的元素,則將導致未定義的行為。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看