Thông tin tài liệu:
Tham khảo tài liệu beginning visual c++® 2005 (p4), công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Beginning Visual C++® 2005 (P4) Arrays, Strings, and Pointers The argument is the name of the array containing the string and the strlen() function returns the length of the string as an unsigned integer of type size_t. Many standard library functions return a value of type size_t and the size_t type is defined within the standard library using a typedef state- ment to be equivalent to one of the fundamental types, usually unsigned int. The reason for using size_t rather than a fundamental type directly is that it allows flexibility in what the actual type is in different C++ implementations. The C++ standard permits the range of values accommodated by a fun- damental type to vary to make the best of a given hardware architecture and size_t can be defined to be the equivalent the most suitable fundamental type in the current machine environment. Finally in the example, the string and the character count is displayed with a single output statement. Note the use of the escape character ‘”’ to output a double quote.Multidimensional Arrays The arrays that you have defined so far with one index are referred to as one-dimensional arrays. An array can also have more than one index value, in which case it is called a multidimensional array. Suppose you have a field in which you are growing bean plants in rows of 10, and the field contains 12 such rows (so there are 120 plants in all). You could declare an array to record the weight of beans pro- duced by each plant using the following statement: double beans[12][10]; This declares the two-dimensional array beans, the first index being the row number, and the second index the number within the row. To refer to any particular element requires two indices. For example, you could set the value of the element reflecting the fifth plant in the third row with the following statement: beans[2][4] = 10.7; Remember that the index values start from zero, so the row index value is 2 and the index for the fifth plant within the row is 4. Being a successful bean farmer, you might have several identical fields planted with beans in the same pattern. Assuming that you have eight fields, you could use a three-dimensional array to record data about these, declared thus: double beans[8][12][10]; This records production for all of the plants in each of the fields, the leftmost index referencing a particu- lar field. If you ever get to bean farming on an international scale, you are able to use a four-dimensional array, with the extra dimension designating the country. Assuming that you’re as good a salesman as you are a farmer, growing this quantity of beans to keep up with the demand may well start to affect the ozone layer. Arrays are stored in memory such that the rightmost index value varies most rapidly. Thus the array data[3][4] is three one-dimensional arrays of four elements each. The arrangement of this array is illustrated in Figure 4-4. The elements of the array are stored in a contiguous block of memory, as indicated by the arrows in Figure 4-4. The first index selects a particular row within the array and the second index selects an ele- ment within the row. 169Chapter 4 data[0][0] data[0][1] data[0][2] data[0][3] data[1][0] data[1][1] data[1][2] data[1][3] data[2][0] data[2][1] data[2][2] data[2][3] The array elements are stored in contiguous locations in memory. Figure 4-4 Note that a two-dimensional array in native C++ is really a one-dimensional array of one-dimensional arrays. A native C++ array with three dimensions is actually a one-dimensional array of elements where each element is a one-dimensional array of on-dimensional arrays. This is not something you need to worry about most of the time, but as you will see later, C++/CLI arrays are not the same as this. It also implies that for the array in Figure 4-4 the expressions data[0], data[1], and data[2], represent one- dimensional arrays.Initializing Multidimensional Arrays To initialize a multidimensional array, you use an extension of the method used for a one-dimensional array. For example, you can initialize a two-dimensional array, data, with the following declaration: long data[2][4] = { { 1, 2, 3, 5 }, { 7, 11, 13, 17 } }; Thus, the initializing values for each row of the array are contained within their own pair of braces. Because there are four elements in each row, there are four initializing values in each group, and because there ar ...