數組元素可以通過數組名稱加索引進行通路,元素的索引是放在方括号内,跟在數組名稱的後邊,例如:
double salary=balance[9];
上面的語句将把數組中第十個元素的值賦給salary變量,下面的執行個體使用了上述的三個概念,即聲明數組,數組指派,通路數組
#include <iostream>
using namespace std;
#include <iomanip>
using std::setw;
int main()
{
int n[10]
for (int i=0;i<10;i++)
{
n[i]=i+100;
}
cout<<"element"<<setw(13)<<"Value"<<endl;
for (int j=0;j<10;j++)
{
cout<<setw(7)<<j<<setw(13)<<n[j]<<endl;
}
return 0;
}
上面的程式使用了setw()函數來格式化輸出,當上面的代碼被編譯和執行時,他會如清單般列印出來