Online Book Reader

Home Category

Professional C__ - Marc Gregoire [179]

By Root 1566 0
to shared_ptr in the earlier STL, although many third-party libraries (for example Boost) did provide this capability.

If your compiler supports the new C++11 unique_ptr and shared_ptr, it is highly recommended to use them instead of the deprecated auto_ptr.

Chapter 21 discusses smart pointers in more detail.

Exceptions

The C++ language supports exceptions, which allow functions or methods to pass errors of various types up to calling functions or methods. The C++ standard library provides a class hierarchy of exceptions that you can use in your program as is, or that you can subclass to create your own exception types. Chapter 10 covers the details of exceptions and the standard exception classes.

Mathematical Utilities

The C++ library provides some mathematical utility classes. Although they are templatized so that you can use them with any type, they are not generally considered part of the standard template library. Unless you are using C++ for numeric computation, you will probably not need to use these utilities.

The standard library provides a complex number class, called complex, which provides an abstraction for working with numbers that contain both real and imaginary components.

The standard library also contains a class called valarray, which is similar to the vector class but is more optimized for high performance numerical applications. The library provides several related classes to represent the concept of vector slices. From these building blocks, it is possible to build classes to perform matrix mathematics. There is no built-in matrix class; however, there are third-party libraries like Boost that include matrix support.

C++ also provides a new way to obtain information about numeric limits, such as the maximum possible value for an integer on the current platform. In C, you could access #defines, such as INT_MAX. While those are still available in C++, you can also use the new numeric_limits template class defined in the header file. Its use is straightforward as is shown in the following code:

cout << "Max int value: " << numeric_limits::max() << endl;

cout << "Lowest int value: " << numeric_limits::lowest() << endl;

cout << "Max double value: " << numeric_limits::max() << endl;

cout << "Lowest double value: " << numeric_limits::lowest() << endl;

Code snippet from numeric_limits\numeric_limits.cpp

Time Utilities

C++11 adds the Chrono library to the standard. It is defined in the header file. This library makes it easy to work with time; for example, to time certain durations or to perform actions based on timing. The Chrono library is discussed in detail in Chapter 16.

Random Numbers

C++ already had support for generating pseudo-random numbers; however, it was only very basic support. For example, you could not change the distribution of the generated random numbers.

C++11 adds a complete random number library, which is much more powerful than the old C++ way of generating random numbers. The new library comes with uniform random number generators, random number engines, random number engine adaptors, and random number distributions. All of these can be used to give you random numbers more suited to your problem domain, such as normal distributions, negative exponential distributions, etc.

Consult Chapter 16 for all the details on this new library.

Compile-Time Rational Arithmetic

The compile-time rational arithmetic library is new to C++11 and provides a ratio template class defined in the header file. This ratio class can exactly represent any finite rational number defined by a numerator and denominator. This library is discussed in Chapter 16.

Tuples

Tuples are sequences with a fixed size that can have heterogeneous elements and are defined in the header file. All standard template library containers discussed further in this chapter store homogenous elements, meaning that all the elements in a container must have the same type. A tuple allows you to store elements of completely unrelated types in one object.

Return Main Page Previous Page Next Page

®Online Book Reader