Online Book Reader

Home Category

Professional C__ - Marc Gregoire [10]

By Root 1280 0
numbered compartment. C++ allows multi-dimensional arrays. You might think of a two-dimensional array as a checkerboard, where each location has a position along the x-axis and a position along the y-axis. Three-dimensional and higher arrays are harder to picture and are rarely used. The following code shows the syntax for allocating a two-dimensional array of characters for a Tic-Tac-Toe board and then putting an “o” in the center square.

char ticTacToeBoard[3][3];

ticTacToeBoard[1][1] = 'o';

Figure 1-1 shows a visual representation of this board with the position of each square.

FIGURE 1-1

In C++, the first element of an array is always at position 0, not position 1! The last position of the array is always the size of the array minus 1!

std::array

C++11 introduces a new type of container called std::array, defined in the header file. This is a replacement for the previously discussed standard C-style arrays. If you wanted a more secure type of array before C++11, you would use the std::vector. The std::vector will automatically grow in size when you add new elements to it. However, sometimes you don’t need this flexibility because you know the exact size. The only way to avoid the std::vector overhead before C++11 was to switch to standard C-style arrays as explained in the previous section. With C++11 you can switch to the more secure std::array instead, which does not have the same overhead as std::vector; it’s basically a tiny wrapper around standard arrays. There are a number of advantages in using std::arrays instead of standard C-style arrays. They always know their own size, do not automatically get cast to a pointer to avoid certain types of bugs and have iterators to easily loop over the elements. Because of the support for iterators, the STL algorithms also work on std::arrays. STL algorithms are explained in detail in Chapter 13.

The following example demonstrates how to use the std::array container.

#include

#include

using namespace std;

int main()

{

array arr = {9, 8, 7};

cout << "Array size = " << arr.size() << endl;

for (auto i : arr)

cout << i << endl;

cout << *iter << endl;

return 0;

}

Code snippet from std_array\std_array.cpp

Both the standard arrays and the new std::arrays have a fixed size, which should be known at compile time. They cannot grow or shrink at run time.

Functions

For programs of any significant size, placing all the code inside of main() is unmanageable. To make programs easy to understand, you need to break up, or decompose, code into concise functions.

In C++, you first declare a function to make it available for other code to use. If the function is used inside a particular file of code, you generally declare and define the function in the source file. If the function is for use by other modules or files, you generally put the declaration in a header file and the definition in a source file.

Function declarations are often called “function prototypes” or “signatures” to emphasize that they represent how the function can be accessed, but not the code behind it.

A function declaration is shown below. This example has a return type of void, indicating that the function does not provide a result to the caller. The caller must provide two arguments for the function to work with — an integer and a character.

void myFunction(int i, char c);

Without an actual definition to match this function declaration, the link stage of the compilation process will fail because code that makes use of the function myFunction() will be calling nonexistent code. The following definition prints the values of the two parameters.

void myFunction(int i, char c)

{

std::cout << "the value of i is " << i << std::endl;

std::cout << "the value of c is " << c << std::endl;

}

Elsewhere in the program, you can make calls to myFunction() and pass in constants or variables for the two parameters. Some sample function calls are shown here:

myFunction(8, 'a');

myFunction(someInt, 'b');

myFunction(5, someChar);

In C++, unlike C, a

Return Main Page Previous Page Next Page

®Online Book Reader