ca27a_demo_数组c++

//27_CppPrimer_数组_txwtech_
/* 
数组的缺点-长度固定。大小不能变。建议用动态数组vector,96年开始标准库
数组的定义与初始化
显示初始化数组元素
特殊的字符数组
不允许数组直接复制和赋值
数组操作:使用下标
检查数组下标值,不要越界。“缓冲区溢出”
*/

//27_CppPrimer_数组_txwtech_
/* 
数组的缺点-长度固定。大小不能变。建议用vector,96年开始标准库
数组的定义与初始化
显示初始化数组元素
特殊的字符数组
不允许数组直接复制和赋值
数组操作:使用下标
检查数组下标值,不要越界。“缓冲区溢出”
*/
#include <iostream>
#include <string>


//局部数组不会初始化
using std::cout;
using std::endl;
using std::string;
unsigned get_size()
{
	int a = 100;
	int b = 200;
	return a + b;
}
int aat1[30];//全局数组,自动初始化为0;

int main()
{
	int a[100];
	const unsigned buf_size = 512, max_files = 20;
	int staff_size = 27;
	const unsigned sz = get_size();

	char input_buffer[buf_size];
	string fileTable[max_files + 1];

	//double salaries[staff_size];//数组维数不能放变量
	//int test_scores[get_size()];//数组维数不能放函数
	//int vals[sz]; //sz要在编译时必须要有值

	const unsigned array_size = 3;
	//int ia[array_size];//里面默认没有值
	int ia[array_size] = { 12,9,37 };

	//int ib[3] = {1,2,3,4,5};//元素数量不匹配
	int ic[5] = {11,22,33};//后面两个默认0
	cout << ia[0] << "," << ia[1] << "," << ia[2] << endl;
	cout << ic[0] << "," << ic[1] << "," << ic[2] <<", "<< ic[3] << "," << ic[4] << endl;

	string str_arr[5] = {"hi","bye"};
	char cal[] = {'C','+','+'};
	char ca2[] = { 'C','+','+','\0' };//c语言风格的字符串 ,\0就是null
	char ca3[] = "C++";//自动加上\0,是看不见的

	//const char ca4[6] = "Daniel"; //实际7个放不下6个
	cout << "数组ok!" << endl;
	return 0;
}
#include <iostream>
using namespace std;

int main()
{
	//const int array_size = 7;
	const size_t array_size = 7;//size_t专门用来做数组下标的
	int ia1[] = {1,2,3,4,5,6,7};
	int ia2[array_size];

	//for (int ix = 0; ix != 7; ++ix)
	for (size_t ix = 0; ix != 7; ++ix)//size_t专门用来做数组下标的
		cout << ia1[ix] << endl;
	//ia2 = ia1;//不能把数组ia1里的数据copy到ia2;

	//用循环进行拷贝
	for (size_t ix = 0; ix != array_size; ++ix)
		ia2[ix] = ia1[ix];
	return 0;
}
发布了354 篇原创文章 · 获赞 186 · 访问量 89万+

猜你喜欢

转载自blog.csdn.net/txwtech/article/details/104088515