Cplusplus 简明教程
C++ Arrays
C++ 提供了一个数据结构 the array ,它存储着固定大小的相同类型元素的连续集合。阵列用于存储数据集合,但通常将阵列视为相同类型变量的集合更为有用。
C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
不要声明诸如 number0、number1、… 和 number99 这样的独立变量,而是声明一个阵列变量,如 numbers,并使用 numbers[0]、numbers[1] 和…,numbers[99] 来表示独立变量。阵列中的特定元素通过索引进行访问。
Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is accessed by an index.
所有阵列都包含连续的内存位置。最低地址对应于第一个元素,而最高地址对应于最后一个元素。
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
Declaring Arrays
要在 C++ 中声明一个阵列,程序员需要按照如下方式指定元素的类型和阵列所需元素的数量 −
To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows −
type arrayName [ arraySize ];
这称为单维阵列。 arraySize 必须大于零的整数常量, type 可以是任何有效的 C++ 数据类型。例如,要声明一个名为 balance 的 10 元素阵列,类型为 double,请使用此语句 −
This is called a single-dimension array. The arraySize must be an integer constant greater than zero and type can be any valid C++ data type. For example, to declare a 10-element array called balance of type double, use this statement −
double balance[10];
Initializing Arrays
你可以逐个地或使用单一陈述初始化 C++ 数组元素,如下所示 −
You can initialize C++ array elements either one by one or using a single statement as follows −
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
大括号 {} 之间的值的数量不能大于我们在方括号 [ ] 之间为数组声明的元素的数量。以下是分配数组的单元元素的一个示例 −
The number of values between braces { } can not be larger than the number of elements that we declare for the array between square brackets [ ]. Following is an example to assign a single element of the array −
如果你省略数组的大小,就会创建一个仅足够容纳初始化大小的数组。因此,如果写入:
If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write −
double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
你会创建与上一个示例中完全相同的数组。
You will create exactly the same array as you did in the previous example.
balance[4] = 50.0;
上面的语句将数组中第 5 个元素指定为值 50.0。索引为 4 的数组将是第 5 个,即最后一个元素,因为所有数组都将 0 作为其第一个元素的索引,这也称为基准索引。以下是对我们上面讨论的同一个数组的图片表示:
The above statement assigns element number 5th in the array a value of 50.0. Array with 4th index will be 5th, i.e., last element because all arrays have 0 as the index of their first element which is also called base index. Following is the pictorial representaion of the same array we discussed above −
Accessing Array Elements
通过给数组名称编制索引来访问元素。这是通过在数组名称后面方括号内放置元素的索引来完成的。例如:
An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example −
double salary = balance[9];
上面的语句将从数组中获取第 10 个元素并将值指定给 salary 变量。以下是一个示例,它将使用上面提到的所有三个概念,即数组的声明、指定和访问:
The above statement will take 10th element from the array and assign the value to salary variable. Following is an example, which will use all the above-mentioned three concepts viz. declaration, assignment and accessing arrays −
#include <iostream>
using namespace std;
#include <iomanip>
using std::setw;
int main () {
int n[ 10 ]; // n is an array of 10 integers
// initialize elements of array n to 0
for ( int i = 0; i < 10; i++ ) {
n[ i ] = i + 100; // set element at location i to i + 100
}
cout << "Element" << setw( 13 ) << "Value" << endl;
// output each array element's value
for ( int j = 0; j < 10; j++ ) {
cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
}
return 0;
}
该程序使用 setw() 函数来设置输出格式。当上面代码被编译和执行时,它产生以下结果:
This program makes use of setw() function to format the output. When the above code is compiled and executed, it produces the following result −
Element Value
0 100
1 101
2 102
3 103
4 104
5 105
6 106
7 107
8 108
9 109
Arrays in C++
数组对于 C 语言很重要,并且需要更多详细的说明。以下几个重要概念对于 C 语言程序员来说应该是清晰的:
Arrays are important to C and should need lots of more detail. There are following few important concepts, which should be clear to a C programmer −
Sr.No |
Concept & Description |
1 |
Multi-dimensional arraysC++ supports multidimensional arrays. The simplest form of the multidimensional array is the two-dimensional array. |
2 |
Pointer to an arrayYou can generate a pointer to the first element of an array by simply specifying the array name, without any index. |
3 |
Passing arrays to functionsYou can pass to the function a pointer to an array by specifying the array’s name without an index. |
4 |
Return array from functionsC++ allows a function to return an array. |