/*
* C++中提供了两种类似于ector 和迭代器类型的低级复合类型--数组和指针。现代C++
* 程序尽量使用vector和迭代器类型,而避免使用低级的数组和指针。设计良好的程序
* 只有在强调速度时才在类实现的内部使用数组和指针。
*
* 定义数组的维数必须用值大于等于1的常量表达式定义。
* 与vector不同,一个数组不能直接复制和赋值。
* 在用下标访问元素时,vctor使用vector::size_type 作为下标的类型,而数组下标
* 的正确类型则是 size_t.
*
*/
# include <iostream>
# include <cstddef> // for the definition of size_t
using namespace std;
int main(){
const size_t array_size = 10;
int a1[array_size]; //ten elements of int type are uninitialized
int a2[array_size];
//loop through array, assigning value of its index to each elements
cout<<"the array a1: "<<endl;
for(size_t ix = 0; ix != array_size; ++ix){
a1[ix] = ix;
cout<<a1[ix]<<" ";
}
cout<<endl;
//copy elements from a1 into a2
cout<<"the array a2: "<<endl;
for(ix = 0; ix != array_size; ++ix){
a2[ix] = a1[ix];
cout<<a1[ix]<<" ";
}
cout<<endl;
return 0;
}
/*
the result
----------------------------------------
the array a1:
0 1 2 3 4 5 6 7 8 9
the array a2:
0 1 2 3 4 5 6 7 8 9
Press any key to continue
*/
c++ 中的简单复合内置类型 数组
最新推荐文章于 2024-06-29 17:15:49 发布