C ++中数组的默认值是什么? [英] what is the default value of an array in C++?

查看:115
本文介绍了C ++中数组的默认值是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我要使用 int * array = new int [10]; 创建一个数组,并用值填充该数组的一部分,那么如何检查该数组有多少被填充?我想遍历并检查每个值是否是默认值,但是我不确定每个数组元素的默认值是什么.是 null 0 还是垃圾值?

If I were to create an array with int* array = new int[10]; and fill part of the array with values, how can I check how much of the array is filled? I want to loop through and check if each value is the default value but I'm not sure what the default value of each array element is. Would it be null 0 or garbage values?

推荐答案

这是在创建数组时如何在C ++中设置默认值的方法.

This is how to set a default value in C++ when making an array.

int array[100] = {0};

现在,每个元素都设置为 0 .如果不这样做,每个元素都会浪费,并且如果使用的话将是未定义的行为.

Now every element is set to 0. Without doing this every element it garbage and will be undefined behavior if used.

并非所有的语言都是这样.声明数据结构时,Java具有默认值,而C ++没有.

Not all languages are like this. Java has default values when declaring a data structure but C++ does not.

这篇关于C ++中数组的默认值是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆