1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【C++】基礎を学ぶ⑬~配列~

Last updated at Posted at 2022-07-10

配列

配列とは、複数の値を保存できる基本的な変数型の一つ
配列を作成するときは要素数を最初に指定する必要がある

配列の宣言

要素の型 配列名[要素数];

配列の初期化

要素の型 配列名[要素数] = {要素1, 要素2, ...};

配列要素の取得

配列名[要素の場所];

配列を使ったプログラム

#include <iostream>

using namespace std;

int main() {
  int array[5] = {10, 20, 30, 40, 50};
  cout << array[0] << endl;
  cout << array[2] << endl;
  cout << array[4] << endl;
  return 0;
}
10
30
50

範囲for

配列のすべての要素に処理を行う場合、範囲for文を使うとプログラムが簡潔に書くことができる

範囲for文の使い方

for (配列の要素の型 変数名 : 配列変数) {
  // 各要素に対する処理
}
#include <iostream>

using namespace std;

int main() {
  int array[5] = {10, 20, 30, 40, 50};
  for (int x : array) {
    cout << x << endl;
  }
  return 0;
}
10
20
30
40
50

次の記事

【C++】基礎を学ぶ⑭~ポインタ~

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?