数组array

C++ defines two lower-level compound types arrays and pointers that are similar to vector s and iterators. Modern C++ programs should almost always  use vector s and iterators in preference to the lower-level arrays and pointers. Well-designed programs use arrays and pointers only in the internals of class implementations where speed is essential.

A variable of array type has three important limitations: Its size is fixed, the size must be known at compile time, and the array exists only until the end of the block in which it was defined.

The type of specifier can denote a built-in data or class type. With the exception of references, the element type of an array can also be any compound type. There are no arrays of references.

---------------------------------插曲------------------------------------

1、常量在定义后就不能修改,所以定义时必须初始化。

int a=10;
const int b = a;
或者
const int a = 10;

2、在全局作用域里,非const变量默认为extern。要使const变量能在其他文件中访问,必须显示地将其指定为extern。

---------------------------------插曲------------------------------------

A nonconst variable, or a const variable whose value is not known until run time, cannot be used to specify the dimension of an array.

// both buf_size and max_files are const
const int n;      //error,const object must be initialized
const unsigned buf_size = 512, max_files = 20;
int staff_size = 27; // nonconst
const unsigned sz = get_size(); // const value not known until run time
char input_buffer[buf_size]; // ok: const variable
string fileTable[max_files + 1]; // ok: constant expression
double salaries[staff_size]; // error: non const variable
int test_scores[get_size()]; // error: non const expression
int vals[sz]; // error: size not known until run time

Although staff_size is initialized with a literal constant, staff_size itself is a nonconst object. Its value can be known only at run time, so it is illegal as an array dimension. Even though size is a const object, its value is not known until get_size is called at run time. Therefore, it may not be used as a dimension. On the other hand, the expression max_files + 1 is a constant expression because max_files is a const variable. The expression can be and is evaluated at compile time to a value of 21.

1、Elements of an array of built-in type defined outside the body of a function are initialized to zero.
2、Elements of an array of built-in type defined inside the body of a function are uninitialized.
3、Regardless of where the array is defined, if it holds elements of a class type, then the elements are initialized by the default constructor for that class if it has one. If the class does not have a default constructor, then the elements must be explicitly initialized.

If the dimension size is greater than the number of listed elements, the initializers are used for the first elements. The remaining elements are initialized to zero if the elements are of built-in type or by running the default constructor if they are of class type:

const unsigned array_size = 5;
// Equivalent to ia = {0, 1, 2, 0, 0}
// ia[3] and ia[4] default initialized to 0
int ia[array_size] = {0, 1, 2};
// Equivalent to str_arr = {"hi", "bye", "", "", ""}
// str_arr[2] through str_arr[4] default initialized to the empty string
string str_arr[array_size] = {"hi", "bye"};

 

When we allocate an array of objects of a class type, then that type's default constructor  is used to initialize each element. If the array holds elements of built-int ype, then the elements are uninitialized:

string *psa = new string[10]; // array of 10 empty strings
int *pia = new int[10]; // array of 10 uninitialized ints

It Is Legal to Dynamically Allocate an Empty Array

When we dynamically allocate an array, we often do so because we don't know the size of the array at compile time. We might write code such as

size_t n = get_size(); // get_size returns number of elements needed
int* p = new int[n];
for (int* q = p; q != p + n; ++q)
/* process the array */ ;

The language specifies that a call to new to create an array of size zero is legal. It is legal even though we could not create an array variable of size 0:

char arr[0]; // error: cannot define zero-length array
char *cp = new char[0]; // ok: but cp can't be dereferenced

When we use new to allocate an array of zero size, new returns a valid, nonzero pointer. This pointer will be distinct from any other pointer returned by new . The pointer cannot be dereferencedafter all, it points to no element. The pointer can be compared and so can be used in a loop such as the preceeding one. It is also legal to add (or subtract) zero to such a pointer and to subtract the pointer from itself, yielding zero.

Strictly speaking, there are no multidimensioned arrays in C++. What is commonly referred to as a multidimensioned array is actually an array of arrays:

// array of size 3, each element is an array of ints of size 4
int ia[3][4];

It can be helpful to keep this fact in mind when using what appears to be a multidimensioned array.

Because a multidimensioned array is really an array of arrays, the pointer type to which the array converts is a pointer to the first inner array.

int ia[3][4]; // array of size 3, each element is an array of ints of size 4
int (*ip)[4] = ia; // ip points to an array of 4 ints
ip = &ia[2]; // ia[2] is an array of 4 ints
int a[3][3];
printf("%s\n", typeid(a).name());   // int (*)[3],在c++中数组的名字被转化为指向数组第一个元素的指针,并且没有真正的多维数组,有点的只是数组的数组,即数组的每个元素又是一个数组。

Typedefs Simplify Pointers to Multidimensioned Arrays

Typedefs can help make pointers to elements in multidimensioned arrays easier to write, read, and understand. We might write a typedef for the element type of ia as

int ia[3][4];
typedef int int_array[4];
int_array *ip = ia;

We might use this typedef to print the elements of ia :

for (int_array *p = ia; p != ia + 3; ++p)
         for (int *q = *p; q != *p + 4; ++q)
                  cout << *q << endl;

 

posted on 2014-04-22 21:28  江在路上2  阅读(138)  评论(0)    收藏  举报