20251221_133301 數組的定義與使用

認識數組

相等於scratch中的列表 它的特點是 有序,起點是0 有空間的,不是無限空間 成員的一致性

數組定義 先定義後賦值

#include <iostream>
using namespace std;

int main(){
	// 定義數組 
	int nums[5]; 
	
	// 保存數值到數組中
	nums[0] = 66;
	
	// 獲取數組的值
	cout<<nums[0];  
}

數組定義 邊定義邊賦值

定義了3個位置的整數數組 並存放了數據

#include <iostream>
using namespace std;

int main(){
	int nums[3] = {11,22,33};
	cout<<nums[2];
}

數組定義 邊定義邊賦值2

定義時不限定位置 但是賦值時有多少個值 位置就是多少個

#include <iostream>
using namespace std;

int main(){
	int nums[] = {11,22,33};
	cout<<nums[2];
}