C++中的數(shù)組你真的理解了嗎
1 概述
所謂數(shù)組,就是一個集合,里面存放了相同類型的數(shù)據(jù)元素。
特點1:數(shù)組中的每個數(shù)據(jù)元素都是相同的數(shù)據(jù)類型。
特點2:數(shù)組是由連續(xù)的內存位置組成的。
2 一維數(shù)組
2.1 一維數(shù)組定義方式
一共有三種
1.數(shù)據(jù)類型 數(shù)組名[數(shù)組長度];
2.數(shù)據(jù)類型 數(shù)組名[數(shù)組長度]={值1,值2,值3,...};
3.數(shù)據(jù)類型 數(shù)組名[]={值1,值2,值3,...};
#include<iostream>
using namespace std;
int main()
{
//第一種定義數(shù)組
int arr[5] ;
memset(arr, 0, sizeof(arr));//初始化為0,否則為隨機數(shù)
arr[0] = 10;
arr[1] = 10;
arr[2] = 10;
arr[3] = 10;
arr[4] = 10;
//訪問數(shù)組
for (int i = 0; i < 5; i++)
{
cout << arr[i] << " ";
}
cout << endl;
//第二種定義數(shù)組
//如果在初始化的時候沒有填充完,剩余的會用0來填充。
int brr[5] = { 20,20,20,20 };
for (int i = 0; i < 5; i++)
{
cout << brr[i] << " ";
}
cout << endl;
//第三種定義數(shù)組
char crr[] = { '3','3','3','3','c'};
for (int i = 0; i < 5; i++)
{
cout << crr[i] << " ";
}
system("pause");
return 0;
}
- 第一種方法是先聲明再定義,對于全局/靜態(tài)數(shù)組(定義在main()之外的數(shù)組),數(shù)組內容自動初始化為0。如果是局部的,此時數(shù)組的各元素是隨機數(shù),這時可以用
memset(arr, 0, sizeof(arr));使所有元素初始化為0; - 第二種方法是在定義的時候直接初始化,這時如果在初始化的時候沒有填充完,剩余的會用0來填充。
- 第三種方法在定義的時候不指定長度,這時會默認數(shù)組長度是你所賦的值的數(shù)量。
如果引用的索引超出了數(shù)組長度,也可以輸出,輸出結果是隨機數(shù)(int型數(shù)組)/ 問號(char型)。
2.2 一維數(shù)組組名
一維數(shù)組組名用途:
- 可以統(tǒng)計整個數(shù)組在內存中的長度。
- 可以獲取數(shù)組在內存中的首地址。
注意:數(shù)組名是常量,不可以像更改數(shù)組元素那樣更改數(shù)組名
#include<iostream>
using namespace std;
int main()
{
//1.統(tǒng)計內存
int arr[5] = { 1,2,3,4,5 };
cout << "整個數(shù)組占用內存空間為:" << sizeof(arr) << endl;
cout << "每個元素占用內存空間為:" << sizeof(arr[0]) << endl; //元素內存相同,所以只看一個就可以了。
//2.查看數(shù)組首地址
cout << "數(shù)組的首地址為(十六進制):" << arr << endl;
cout << "數(shù)組的首地址為(十進制):" << (int)arr << endl;
cout << "數(shù)組的第一個元素地址為(十進制):" << (int)&arr[0] << endl;
cout << "數(shù)組的第二個元素地址為(十進制):" << (int)&arr[1] << endl;
//會發(fā)現(xiàn)差4個字節(jié),就是一個整型數(shù)組的內存大小。
//3.數(shù)組名是常量,不可以想更改數(shù)組元素那樣更改數(shù)組名
//arr=crr
system("pause");
return 0;
}
練習案例1

#include<iostream>
using namespace std;
int main()
{
int arr[5] = { 300,350,200,400,250 };
int size = sizeof(arr) / sizeof(arr[0]);
int max = 0;
for (int i = 0; i < size; i++)
{
if (arr[i] > max) { max = arr[i];}
}
cout << "最重的小豬體重為:" << max << endl;
system("pause");
return 0;
}
練習案例2

#include<iostream>
using namespace std;
int main()
{
int arr[5] = { 1,3,5,9,4 };
int size = sizeof(arr) / sizeof(arr[0]);
int start = 0;
int end = size - 1;
int temp = 0;
//打印逆置前的數(shù)組
cout << "數(shù)組逆置前:" << endl;
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
//逆置
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
//打印逆置后的數(shù)組
cout << "數(shù)組逆置后:" << endl;
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
system("pause");
return 0;
}
2.3 冒泡排序
作用:最常用的排序算法,對數(shù)組內元素進行排序。
方法:
- 比較相鄰的元素。如果第一個比第二 個大, 就交換他們兩個。
- 對每一對相鄰元素做同樣的工作,執(zhí)行完畢后,找到第一個最大值。
- 重復以上的步驟,每次比較次數(shù)-1,直到不需要比較
例如:排序{4,2,8,0,5,7,1,3,9}

#include<iostream>
using namespace std;
int main()
{
int arr[] = { 4,8,0,5,7,1,3,0};
int size = sizeof(arr) / sizeof(arr[0]);
//打印排序前的數(shù)組
cout << "數(shù)組排序前:" << endl;
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
//冒泡排序
for (int i = 0; i < size-1; i++) //從0開始,共有size-1輪
{
for (int j = 0; j + i < size - 1; j++) //每輪的比較次數(shù)與當前輪數(shù)相加小于size-1
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j + 1] = temp;
}
}
}
cout << "數(shù)組排序后:" << endl;
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
system("pause");
return 0;
}
3 二維數(shù)組
二維數(shù)組就是在一維數(shù)組上,多加一個維度。
3.1 二維數(shù)組定義方式
1.數(shù)據(jù)類型 數(shù)組名[行數(shù)][列數(shù)];
2.數(shù)據(jù)類型 數(shù)組名[行數(shù)][列數(shù)]={{數(shù)據(jù)1,數(shù)據(jù)2},{數(shù)據(jù)3,數(shù)據(jù)4}};
3.數(shù)據(jù)類型 數(shù)組名[行數(shù)][列數(shù)]={數(shù)據(jù)1,數(shù)據(jù)2,數(shù)據(jù)3,數(shù)據(jù)4};
4.數(shù)據(jù)類型 數(shù)組名[][列數(shù)]={數(shù)據(jù)1,數(shù)據(jù)2,數(shù)據(jù)3,數(shù)據(jù)4};
一般都是使用第二種,因為第二種最直觀,提高代碼的可讀性。
#include<iostream>
using namespace std;
int main()
{
//1.數(shù)據(jù)類型 數(shù)組名[行數(shù)][列數(shù)];
//2.數(shù)據(jù)類型 數(shù)組名[行數(shù)][列數(shù)] = { {數(shù)據(jù)1,數(shù)據(jù)2},{數(shù)據(jù)3,數(shù)據(jù)4} };
//3.數(shù)據(jù)類型 數(shù)組名[行數(shù)][列數(shù)] = { 數(shù)據(jù)1,數(shù)據(jù)2,數(shù)據(jù)3,數(shù)據(jù)4 };
//4.數(shù)據(jù)類型 數(shù)組名[][列數(shù)] = { 數(shù)據(jù)1,數(shù)據(jù)2,數(shù)據(jù)3,數(shù)據(jù)4 };
//1.
int arr[2][3];
arr[0][0] = 0;
arr[0][1] = 1;
arr[0][2] = 2;
arr[1][0] = 3;
arr[1][1] = 4;
arr[1][2] = 5;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr[i][j] << "\t";
}
cout << endl;
}
cout << endl;
//2.
int brr[2][3] =
{
{1,2,3},
{4,5,6}
};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << brr[i][j] << "\t";
}
cout << endl;
}
cout << endl;
//3.
int crr[2][3] = { 1,2,3,4,5,6 };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << crr[i][j] << "\t";
}
cout << endl;
}
cout << endl;
//4.
int drr[][3] = { 1,2,3,4,5,6 };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << drr[i][j] << "\t";
}
cout << endl;
}
system("pause");
return 0;
}
3.2 二維數(shù)組數(shù)組名
查看二維數(shù)組所占內存空間或者某行占用內存空間
#include<iostream>
using namespace std;
int main()
{
int arr[2][3] =
{
{1,2,3},
{4,5,6}
};
cout << "二維數(shù)組占用內存為:" << sizeof(arr) << endl;
cout << "某一行占用內存為:" << sizeof(arr[0]) << endl;
cout << "一個元素占用內存為:" << sizeof(arr[0][0]) << endl;
int row = sizeof(arr) / sizeof(arr[0]);
int column = sizeof(arr[0]) / sizeof(arr[0][0]);
cout << "數(shù)組的行數(shù)為:" << row << "\n" << "數(shù)組的列數(shù)為:" << column << endl;
cout << "二維數(shù)組的首地址是(16進制):" << arr << endl;
cout << "二維數(shù)組的首地址是(10進制):" << (int)arr << endl;
cout << "第一行首地址是(10進制):" << (int)arr[0] << endl;
cout << "第二行首地址是(10進制):" << (int)arr[1] << endl;
//會發(fā)現(xiàn)第一行和第二行差12,正好三個整型元素
cout << "第二行第一個元素地址是(10進制):" << (int)&arr[1][0] << endl;//直接使用arr[1][0]是查看這個元素內容,需要用&取地址。
system("pause");
return 0;
}
3.3二維數(shù)組應用舉例

#include<iostream>
using namespace std;
//vs快捷鍵crtl+d可以直接把本行復制到下行
int main()
{
int arr[3][3] =
{
{100,100,100},
{90 ,50 ,100},
{60, 70 ,80 }
};
cout << "成績情況為:" << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
string names[3] = { "張三","李四","王五" };
for (int i = 0; i < 3; i++)
{
int sum = 0;
for (int j = 0; j < 3; j++)
{
sum += arr[i][j];
}
cout << names[i] << "的總成績?yōu)椋? << sum << endl;
}
system("pause");
return 0;
}
總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內容!
相關文章
C語言實現(xiàn)魔方陣算法(幻方陣 奇魔方 單偶魔方實現(xiàn))
魔方陣是指由1,2,3……n2填充的,每一行、每一列、對角線之和均相等的方陣,階數(shù)n = 3,4,5…。魔方陣也稱為幻方陣,看下面的實現(xiàn)方法吧2013-11-11
C語言統(tǒng)計一篇英文短文中單詞的個數(shù)實例代碼
本文通過實例代碼給大家介紹的C語言統(tǒng)計一篇英文短文中單詞的個數(shù),代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2018-03-03
解析C++中的for循環(huán)以及基于范圍的for語句使用
這篇文章主要介紹了解析C++中的for循環(huán)以及基于范圍的for語句使用,是C++入門學習中的基礎知識,需要的朋友可以參考下2016-01-01

