• Complain

Ivor Horton - Using the C++ Standard Template Libraries

Here you can read online Ivor Horton - Using the C++ Standard Template Libraries full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2015, publisher: Apress, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Ivor Horton Using the C++ Standard Template Libraries
  • Book:
    Using the C++ Standard Template Libraries
  • Author:
  • Publisher:
    Apress
  • Genre:
  • Year:
    2015
  • Rating:
    4 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 80
    • 1
    • 2
    • 3
    • 4
    • 5

Using the C++ Standard Template Libraries: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Using the C++ Standard Template Libraries" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

Ivor Horton: author's other books


Who wrote Using the C++ Standard Template Libraries? Find out the surname, the name of the author of the book and a list of all author's works by series.

Using the C++ Standard Template Libraries — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Using the C++ Standard Template Libraries" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Ivor Horton 2015
Ivor Horton Using the C++ Standard Template Libraries 10.1007/978-1-4842-0004-9_1
1. Introducing the Standard Template Library
Ivor Horton 1
(1)
WA, UK
Electronic supplementary material
The online version of this chapter (doi: 10.1007/978-1-4842-0004-9_1 ) contains supplementary material, which is available to authorized users.
This chapter explains the fundamental ideas behind the Standard Template Library (STL). This is to give you an overall grasp of how the various types of entities in the STL hang together. Youll see more in-depth examples and discussion of everything that I introduce in this chapter in the book. In this chapter youll learn the following:
  • What is in the STL
  • How templates are defined and used
  • What a container is
  • What an iterator is and how it is used
  • The importance of smart pointers and their use with containers
  • What algorithms are and how you apply them
  • What is provided by the numerics library
  • What a function object is
  • How lambda expressions are defined and used
Besides introducing the basic ideas behind the STL, this chapter provides brief reminders of some C++ language features that you need to be comfortable with because they will be used frequently in subsequent chapters. You can skip any of these sections if you are already familiar with the topic.
Basic Ideas
The STL is an extensive and powerful set of tools for organizing and processing data. These tools are all defined by templates so the data can be of any type that meets a few minimum requirements. Im assuming that you are reasonably familiar with how class templates and function templates can be defined and how they are used, but Ill remind you of the essentials of these in the next section. The STL can be subdivided into four conceptual libraries:
  • The Containers Library defines containers for storing and managing data. The templates for this library are defined within the following header files: array , vector , stack , queue , deque , list , forward_list , set , unordered_set , map , and unordered_map .
  • The Iterators Library defines iterators, which are objects that behave like pointers and are used to reference sequences of objects in a container. The library is defined within a single header file, iterator .
  • The Algorithms Library defines a wide range of algorithms that can be applied to a set of elements stored in a container. The templates for this library are defined in the algorithm header file.
  • The Numerics Library defines a wide range of numerical functions, including numerical processing of sets of elements in a container. The library also includes advanced functions for random number generation. The templates for this library are defined in the headers complex , cmath , valarray , numeric , random , ratio , and cfenv . The cmath header has been around for a while, but it has been extended in the C++ 11 standard and is included here because it contains many mathematical functions.
Many complex and difficult tasks can be achieved very easily with remarkably few lines of code using the STL. For instance, without explanation, heres the code to read an arbitrary number of floating-point values from the standard input stream and calculate and output the average:
std::vector values;
std::cout << "Enter values separated by one or more spaces. Enter Ctrl+Z to end:\n ";
values.insert(std::begin(values), std::istream_iterator(std::cin),
std::istream_iterator());
std::cout << "The average is "
<< (std::accumulate(std::begin(values), std::end(values), 0.0)/values.size())
<< std::endl;
It requires only four statements! Long lines admittedly, but no loops are required; its all taken care of by the STL. This code can be easily modified to do the same job with data from a file. Because of the power and wide applicability of the STL, its a must for any C++ programmers toolbox. All STL names are in the std namespace so I wont always qualify STL names explicitly with std in the text. Of course, in any code I will qualify names where necessary.
Templates
A template is a parametric specification of a set of functions or classes. The compiler can use a template to generate a specific function or class definition when necessary, which will be when you use the function template or class template type in your code. You can also define templates for parameterized type aliases. Thus a template is not executable code it is a blueprint or recipe for creating code. A template that is never used in a program is ignored by the compiler so no code results from it. A template that is not used can contain programming errors, and the program that contains it will still compile and execute; errors in a template will not be identified until the template is used to create code that is then compiled.
A function or class definition that is generated from a template is an instance or an instantiation of the template. Template parameter values are usually data types, so a function or class definition can be generated for a parameter value of type int, for example, and another definition with a parameter value of type string . Parameter arguments are not necessarily types; a parameter specification can be an integer type that requires an integer argument. Heres an example of a very simple function template:
template T& larger(T& a, T& b)
{
return a > b ? a : b;
}
This is a template for functions that return the larger of the two arguments. The only limitation on the use of the template is that the type of the arguments must allow a > comparison to be executed. The type parameter T determines the specific instance of the template to be created. The compiler can deduce this from the arguments you supply when you use larger() , although you can supply it explicitly. For example:
std::string first {"To be or not to be"};
std::string second {"That is the question."};
std::cout << larger(first, second) << std::endl;
This code requires the string header to be included. The compiler will deduce the argument for T as type string . If you want to specify it, you would write larger(first, second) . You would need to specify the template type argument when the function arguments differ in type. If you wrote larger(2, 3.5), for example, the compiler cannot deduce T because it is ambiguous it could be type int or type double . This usage will result in an error message. Writing larger(2, 3.5) will fix the problem.
Heres an example of a class template:
template class Array
{
private:
T* elements; // Array of type T
size_t count; // Number of array elements
public:
explicit Array(size_t arraySize); // Constructor
Array(const Array& other); // Copy Constructor
Array(Array&& other); // Move Constructor
virtual Array(); // Destructor
T& operator[](size_t index); // Subscript operator
const T& operator[](size_t index) const; // Subscript operator-const arrays
Array& operator=(const Array& rhs); // Assignment operator
Array& operator=(Array&& rhs); // Move assignment operator
size_t size() { return count; } // Accessor for count
};
The size_t type alias is defined in the cstddef header and represents an unsigned integer type. This code defines a simple template for an array of elements of type T . Where Array appears in the template definition Array is implied and you could write this if you wish. Outside the body of the template in an external function member definition, you must write Array . The assignment operator allows one Array object to be assigned to another, which is something you cant do with ordinary arrays. If you wanted to inhibit this capability, you would still need to declare the operator=() function as a member of the template. If you dont, the compiler will create a public default assignment operator when necessary for a template instance. To prevent use of the assignment operator, you should specify it as deleted like this:
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Using the C++ Standard Template Libraries»

Look at similar books to Using the C++ Standard Template Libraries. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Using the C++ Standard Template Libraries»

Discussion, reviews of the book Using the C++ Standard Template Libraries and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.