动态数组中设置默认值 [英] Set default value of dynamic array
问题描述
这code将创造100个元素的数组,每个的值设置为false。
布尔boolArray [100] = FALSE;
我如何设置动态数组的默认值?
无效美孚(INT大小)
{
布尔boolArray =新布尔【尺寸】;
//怎么办?
}
在标准C ++就可以默认初始化任何事情,包括数组:
BOOL * boolArray =新布尔[大小](); //初始化为零
完整的程序,还检查结果,并释放数组:
布尔美孚(INT大小)
{
BOOL * boolArray =新布尔[大小](); //初始化为零 //检查它确实是零初始化:
的for(int i = 0; I<大小; ++ I)
{
如果(boolArray [I]){删除[] boolArray;返回false; }
}
删除[] boolArray;返回true;
}#包括LT&;&iostream的GT;
诠释的main()
{
使用命名空间std;
COUT<< (富(42)确定:解精编译?)所述;&下; ENDL;
}
你的编译器
是否会接受,甚至做正确的事情就另当别论了。
因此,在实践中的如果的你感到一种不可抗拒的冲动,使用原始阵列的话,或许更好的使用的std ::填写
或一些这样,甚至原料循环
但需要注意的重复删除[]
-ex pressions。这些多余的code是非常容易出错的:它的邪恶与贸易;.而且还有很多其他可以去错用生阵列,所以作为一个新手,刚说不与贸易;原始数组和原始指针和这样的。
相反,使用标准库中的容器,它管理分配,初始化,复制和释放你&ndash的;正确。有一个小问题,虽然,在即premature优化的std ::矢量<布尔>
,否则将是自然的选择。从本质上讲的std ::矢量<布尔>
使用每个值只有一个位,所以它不能分发给布尔$ C $引用C>元素,而是转手出去代理对象…
因此,布尔
元素,例如使用一个和std :: bitset
(当大小在编译时已知),或如一个的std :: deque的
,如下:
的#include<&双端GT;布尔美孚(INT大小)
{
的std :: deque的<布尔> boolArray(大小); //初始化为零 的for(int i = 0; I<大小; ++ I)
{
如果(boolArray [I]){返回false; }
}
返回true;
}#包括LT&;&iostream的GT;
诠释的main()
{
使用命名空间std;
COUT<< (富(42)确定:解精编译?)所述;&下; ENDL;
}
&干杯放大器;心连心,
This code will create an array of 100 elements and set the value of each to false.
bool boolArray[100] = false;
How can I set the default value of a dynamic array?
void Foo(int size)
{
bool boolArray = new bool[size];
//Now what?
}
In standard C++ you can default-initialize just about anything, including that array:
bool* boolArray = new bool[size](); // Zero-initialized
Complete program that also checks the result, and deallocates the array:
bool foo( int size )
{
bool* boolArray = new bool[size](); // Zero-initialized
// Check that it is indeed zero-initialized:
for( int i = 0; i < size; ++i )
{
if( boolArray[i] ) { delete[] boolArray; return false; }
}
delete[] boolArray; return true;
}
#include <iostream>
int main()
{
using namespace std;
cout << (foo( 42 )? "OK" : "Ungood compiler") << endl;
}
Whether your compiler will accept or even do the Right Thing is another matter.
So, in practice, if you feel an irresistible urge to use a raw array, then perhaps better use std::fill
or some such, or even a raw loop.
But note the repeated delete[]
-expressions. Such redundant code is very easy to get wrong: it's Evil™. And there's much else that can go wrong with use of raw arrays, so as a novice, just Say No™ to raw arrays and raw pointers and such.
Instead, use standard library containers, which manage allocation, initialization, copying and deallocation for you – correctly. There is a little problem with that, though, namely a premature optimization in std::vector<bool>
, which otherwise would be the natural choice. Essentially std::vector<bool>
uses just one bit per value, so that it can't hand out references to bool
elements, but instead hands out proxy objects…
So, for bool
elements, use e.g. a std::bitset
(when the size is known at compile time), or e.g. a std::deque
, as follows:
#include <deque>
bool foo( int size )
{
std::deque<bool> boolArray( size ); // Zero-initialized
for( int i = 0; i < size; ++i )
{
if( boolArray[i] ) { return false; }
}
return true;
}
#include <iostream>
int main()
{
using namespace std;
cout << (foo( 42 )? "OK" : "Ungood compiler") << endl;
}
Cheers & hth.,
这篇关于动态数组中设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!