c++ 中的简单复合内置类型 数组

/*
 *     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
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值