Mastering C++ Programming - Jeganathan Swaminathan - E-Book

Mastering C++ Programming E-Book

Jeganathan Swaminathan

0,0
45,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Take your C++ coding to the next level by leveraging the latest features and advanced techniques to building high performing, reliable applications.

About This Book



  • Get acquainted with the latest features in C++ 17
  • Take advantage of the myriad of features and possibilities that C++ offers to build real-world applications
  • Write clear and expressive code in C++, and get insights into how to keep your code error-free

Who This Book Is For



This book is for experienced C++ developers. If you are a novice C++ developer, then it's highly recommended that you get a solid understanding of the C++ language before reading this book

What You Will Learn



  • Write modular C++ applications in terms of the existing and newly introduced features
  • Identify code-smells, clean up, and refactor legacy C++ applications
  • Leverage the possibilities provided by Cucumber and Google Test/Mock to automate test cases
  • Test frameworks with C++
  • Get acquainted with the new C++17 features
  • Develop GUI applications in C++
  • Build portable cross-platform applications using standard C++ features

In Detail



C++ has come a long way and has now been adopted in several contexts. Its key strengths are its software infrastructure and resource-constrained applications. The C++ 17 release will change the way developers write code, and this book will help you master your developing skills with C++. With real-world, practical examples explaining each concept, the book will begin by introducing you to the latest features in C++ 17. It encourages clean code practices in C++ in general, and demonstrates the GUI app-development options in C++. You'll get tips on avoiding memory leaks using smart-pointers. Next, you'll see how multi-threaded programming can help you achieve concurrency in your applications. Moving on, you'll get an in-depth understanding of the C++ Standard Template Library. We show you the concepts of implementing TDD and BDD in your C++ programs, and explore template-based generic programming, giving you the expertise to build powerful applications. Finally, we'll round up with debugging techniques and best practices.By the end of the book, you'll have an in-depth understanding of the language and its various facets.

Style and approach



This straightforward guide will help you level up your skills in C++ programming, be it for enterprise software or for low-latency applications like games. Filled with real-world, practical examples, this book will take you gradually up the steep learning curve that is C++.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 322

Veröffentlichungsjahr: 2017

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Mastering C++ Programming

 

 

 

 

 

 

 

 

 

 

Modern C++ 17 at your fingertips

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Jeganathan Swaminathan

 

 

 

 

 

 

 

BIRMINGHAM - MUMBAI

Mastering C++ Programming

Copyright © 2017 Packt Publishing

 

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

 

First published: August 2017

 

Production reference: 1300817

Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.

ISBN 978-1-78646-162-9

 

www.packtpub.com

Credits

Author

Jeganathan Swaminathan

Copy Editor

Gladson Monteiro

Muktikant Garimella

Reviewer

Brandon James

Project Coordinator

Vaidehi Sawant

Commissioning Editor

 

Aaron Lazar

Proofreader

Safis Editing

Acquisition Editor

Denim Pinto

Indexer

Rekha Nair

ContentDevelopmentEditor

Anurag Ghogre

Graphics

Abhinash Sahu

Technical Editor

Madhunikita Sunil Chindarkar

Production Coordinator

Melwyn Dsa

About the Author

Jeganathan Swaminathan, Jegan for short, is a freelance software consultant and founder of TekTutor, with over 17 years of IT industry experience. In the past, he has worked for AMD, Oracle, Siemens, Genisys Software, Global Edge Software Ltd, and PSI Data Systems. He has consulted for Samsung WTD (South Korea) and National Semiconductor (Bengaluru). He now works as a freelance external consultant for Amdocs (India). He works as freelance software consultant and freelance corporate trainer. He holds CSM, CSPO, CSD, and CSP certifications from Scrum Alliance. He is a polyglot software professional and his areas of interest include a wide range of C++, C#, Python, Ruby, AngularJS, Node.js, Kubernetes, Ansible, Puppet, Chef, and Java technologies. He is well known for JUnit, Mockito, PowerMock, gtest, gmock, CppUnit, Cucumber, SpecFlow, Qt, QML, POSIX – Pthreads, TDD, BDD, ATDD, NoSQL databases (MongoDB and Cassandra), Apache Spark, Apache Kafka, Apache Camel, Dockers, Continuous Integration (CI), Continuous Delivery (CD), Maven, Git, cloud computing, and DevOps. You can reach him for any C++, Java, Qt, QML, TDD, BDD, and DevOps-related training or consulting assignments. Jegan is a regular speaker at various technical conferences.

I would like to extend my special thanks to the book's editor, Anurag Ghogre, for his tireless efforts to bring this book to completion, and Denim Pinto for approaching me to write this book.

About the Reviewer

Brandon James is a support escalation engineer who works with troubleshooting, debugging, and implementing identity management solutions for many enterprise customers for both on-premise and cloud solutions. He worked as a technical reviewer on the Microsoft Identity Manager 2016 Handbook by Packt Publishing. Previously, he worked as a Web Site/Web Application Developer designing and developing websites and internal web applications for various enterprise customers. He holds a B.S. in computer engineering and an M.S. in computer science.

www.PacktPub.com

For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

 

https://www.packtpub.com/mapt

Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.

Why subscribe?

Fully searchable across every book published by Packt

Copy and paste, print, and bookmark content

On demand and accessible via a web browser

Customer Feedback

Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review.

If you'd like to join our team of regular reviewers, you can email us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!

This book is dedicated to my wife, Meenakshi, and my lovely children, Nitesh and Sriram.

Table of Contents

Preface

What this book covers

What you need for this book

Who this book is for

Conventions

Reader feedback

Customer support

Downloading the example code

Errata

Piracy

Questions

C++17 Features

C++17 background

What's new in C++17?

What features are deprecated or removed in C++17?

Key features in C++17

Easier nested namespace syntax

New rules for type auto-detection from braced initializer list 

Simplified static_assert 

The std::invoke( ) method

Structured binding

If and Switch local scoped variables

Template type auto-deduction for class templates

Inline variables

Summary

Standard Template Library

The Standard Template Library architecture

Algorithms

Iterators

Containers

Functors

Sequence containers

Array

Code walkthrough

Commonly used APIs in an array

Vector 

Code walkthrough

Commonly used vector APIs

Code walkthrough

Pitfalls of a vector

List 

Commonly used APIs in a list

Forward list

Code walkthrough

Commonly used APIs in a forward_list container

Deque

Commonly used APIs in a deque

Associative containers

Set

Code walkthrough

Commonly used APIs in a set

Map

Code walkthrough

Commonly used APIs in a map

Multiset

Multimap

Unordered sets

Unordered maps

Unordered multisets

Unordered multimaps

Container adapters

Stack

Commonly used APIs in a stack

Queue

Commonly used APIs in a queue

Priority queue

Commonly used APIs in a priority queue

Summary

Template Programming

Generic programming

Function templates

Code walkthrough

Overloading function templates

Code walkthrough

Class template

Code walkthrough

Explicit class specializations

Code walkthrough

Partial template specialization

Summary

Smart Pointers

Memory management

Issues with raw pointers

Smart pointers

auto_ptr

Code walkthrough - Part 1

Code walkthrough - Part 2

unique_ptr

Code walkthrough

shared_ptr

Code walkthrough

weak_ptr

Circular dependency

Summary

Developing GUI Applications in C++

Qt 

Installing Qt 5.7.0 in Ubuntu 16.04

Qt Core

Writing our first Qt console application

Qt Widgets

Writing our first Qt GUI application

Layouts

Writing a GUI application with a horizontal layout

Writing a GUI application with a vertical layout

Writing a GUI application with a box layout

Writing a GUI application with a grid layout

Signals and slots

Using stacked layout in Qt applications

Writing a simple math application combining multiple layouts

Summary

Multithreaded Programming and Inter-Process Communication

Introduction to POSIX pthreads

Creating threads with the pthreads library

How to compile and run

Does C++ support threads natively?

How to write a multithreaded application using the native C++ thread feature

How to compile and run

Using std::thread in an object-oriented fashion

How to compile and run

What did you learn?

Synchronizing threads

What would happen if threads weren't synchronized?

How to compile and run

Let's use mutex

How to compile and run

What is a deadlock?

How to compile and run

What did you learn?

Shared mutex

Conditional variable

How to compile and run

What did you learn?

Semaphore

Concurrency

How to compile and run

Asynchronous message passing using the concurrency support library

How to compile and run

Concurrency tasks

How to compile and run

Using tasks with a thread support library

How to compile and run

Binding the thread procedure and its input to packaged_task 

How to compile and run

Exception handling with the concurrency library

How to compile and run

What did you learn?

Summary

Test-Driven Development

TDD

Common myths and questions around TDD

Does it take more efforts for a developer to write a unit test? 

Is code coverage metrics good or bad?

Does TDD work for complex legacy projects? 

Is TDD even applicable for embedded or products that involve hardware?

Unit testing frameworks for C++

Google test framework

Installing Google test framework on Ubuntu

How to build google test and mock together as one single static library without installing?

Writing our first test case using the Google test framework

Using Google test framework in Visual Studio IDE

TDD in action

Testing a piece of legacy code that has dependency

Summary

Behavior-Driven Development

Behavior-driven development

TDD versus BDD

C++ BDD frameworks

The Gherkin language

Installing cucumber-cpp in Ubuntu

Installing the cucumber-cpp framework prerequisite software

Building and executing the test cases

Feature file

Spoken languages supported by Gherkin

The recommended cucumber-cpp project folder structure

Writing our first Cucumber test case

Integrating our project in cucumber-cpp CMakeLists.txt

Executing our test case

Dry running your cucumber test cases

BDD - a test-first development approach

Let's build and run our BDD test case

It's testing time!

Summary

Debugging Techniques

Effective debugging

Debugging strategies

Debugging tools

Debugging your application using GDB

GDB commands quick reference

Debugging memory leaks with Valgrind

The Memcheck tool

Detecting memory access outside the boundary of an array

Detecting memory access to already released memory locations

Detecting uninitialized memory access

Detecting memory leaks

Fixing the memory leaks

Mismatched use of new and free or malloc and delete

Summary

Code Smells and Clean Code Practices

Code refactoring

Code smell

What is agile?

SOLID design principle

Single responsibility principle

Open closed principle

Liskov substitution principle

Interface segregation

Dependency inversion

Code smell

Comment smell

Long method

Long parameter list

Duplicate code

Conditional complexity

Large class

Dead code

Primitive obsession

Data class

Feature envy

Summary

Preface

C++ is an interesting programming language that has been around for almost three decades now. It is used to develop complex desktop applications, web applications, networking applications, device drivers, kernel modules, embedded applications, and GUI applications using third-party widget frameworks; literally speaking, C++ can be used in any domain.

Ever since I started programming in 1993, I have cherished the good old technical discussions that I had with many of my colleagues and industry experts that I met from time to time. Of all the technical discussions, one topic gets repeated time and again, which is, "Do you think C++ is a relevant programming language today? Should I continue working on C++ or should I move on to other modern programming languages, such as Java, C#, Scala, or Angular/Node.js?"

I have always felt that one should be open to learning other technologies, but that doesn’t mean having to give up on C++. However, the good news is that with the new C++17 features in place, C++ has been reborn and it is going to stay and rock for many more decades, which is my motivation to write this book.

People have always felt that Java will take over C++, but it has continued to stay. The same discussion started again when C# came into the industry and today again when Angular/Node.js and Scala seem to be more attractive for rapid programming. However, C++ has its own, place and no programming language has been able to take over the place of C++ so far.

There are already many C++ books that help you understand the language, but there are very few books that address developing GUI applications in C++, TDD with C++, and BDD with C++.

C++ has come a long way and has now been adopted in several contexts. Its key strengths are its software infrastructure and resource-constrained applications. The C++ 17 release will change the way developers write code, and this book will help you master your developing skills with C++.

With real-world, practical examples explaining each concept, the book will begin by introducing you to the latest features of C++ 17. It will encourage clean code practices in C++, and demonstrate GUI app development options in C++. You will gain insights into how to avoid memory leaks using smart pointers. Next, you will learn how multithreaded programming can help you achieve concurrency in your applications.

Moving on, you’ll also get an in-depth understanding of the C++ Standard Template Library. We'll explain the concepts of implementing TDD and BDD in your C++ programs, along with template-based generic programming, to equip you with the expertise to build powerful applications. Finally, we’ll round the book off with debugging techniques and best practices. By the time you reach the end of the book, you will have an in-depth understanding of the language and its various facets.

What this book covers

Chapter 1, C++17 Features, explains the new features of C++17 and features that have been removed. It also demonstrates key C++17 features with easy-to-understand examples.

Chapter 2, Standard Template Library, gives an overview of STL, demonstrates various containers and iterators, and explains how to apply useful algorithms on the containers. The chapter also touches on the internal data structures used and their runtime efficiencies.

Chapter 3, Template Programming, gives an overview of generic programming and its benefits. It demonstrates writing function templates and class templates, and overloading function templates. It also touches upon writing generic classes, explicit class specializations, and partial specializations.

Chapter 4, Smart Pointers, explains the issues of using raw pointers and motivates the use of smart pointers. Gradually, this chapter introduces you to the usage of auto_ptr, unique_ptr, shared_ptr, and weak_ptr, and explains ways to resolve cyclic dependency issues.

Chapter 5, Developing GUI Applications in C++, gives an overview of Qt and provides you with step-by-step instructions to install Qt on Linux and Windows. The chapter gradually helps you develop impressive GUI applications with interesting widgets and various layouts. 

Chapter 6, Multithreaded Programming and Inter-Process Communication, introduces to the POSIX pthreads library and discusses the native C++ thread library. It also discusses the benefits of using the C++ thread library. Later, it helps you write multithreaded applications, explores ways to manage the threads, and explains the use of synchronization mechanisms. The chapter discusses deadlocks and possible solutions. Toward the end of the chapter, it introduces you to the concurrency library.

Chapter 7, Test-Driven Development, gives a brief overview of TDD and clarifies FAQs on TDD. This chapter provides you with step-by-step instructions to install Google test framework and integrate it with the Linux and Windows platforms. It helps you develop applications using TDD with an easy-to-understand tutorial style.

Chapter 8, Behavior-Driven Development, gives an overview of BDD and guides you through the installation, integration, and configuration of the Cucumber framework on Linux platforms. It also explains Gherkin and helps you write BDD test cases.

Chapter 9, Debugging Techniques, discusses the various strategies and techniques followed in the industry for debugging your application problems. Later, it helps you understand the use of the GDB and Valgrind tools for step by step debugging, watching variables, fixing various memory-related issues, including memory leaks.

Chapter 10, Code Smells and Clean Code Practices, discusses various code smells and refactoring techniques.

What you need for this book

You will need to be equipped with the following tools before you get started with the book:

g++ compiler of version 5.4.0 20160609 or above

GDB 7.11.1

Valgrind 3.11.0

Cucumber-cpp Git 2.7.4

Google test framework (gtest 1.6 or later)

CMake 3.5.1

Ruby 2.5.1

Qt 5.7.0

Bundler v 1.14.6

The OS required is Ubuntu 16.04 64-bit or later. The hardware configuration should at least be of 1 GB RAM and 20 GB ROM. A virtual machine with this configuration should also suffice.

Who this book is for

This ​book ​is ​for ​experienced ​C++ ​developers. ​If ​you ​are ​a ​novice ​C++ developer, ​then ​it’s ​ ​highly ​recommended ​that ​you ​get ​a ​solid understanding ​of ​the ​C++ ​language ​before reading ​this ​book.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply email [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files emailed directly to you. You can download the code files by following these steps:

Log in or register to our website using your email address and password.

Hover the mouse pointer on the

SUPPORT

tab at the top.

Click on

Code Downloads & Errata

.

Enter the name of the book in the

Search

box.

Select the book for which you're looking to download the code files.

Choose from the drop-down menu where you purchased this book from.

Click on

Code Download

.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR / 7-Zip for Windows

Zipeg / iZip / UnRarX for Mac

7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Mastering-Cpp-Programming. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title. To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at [email protected] with a link to the suspected pirated material. We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.

C++17 Features

In this chapter, you will be learning the following concepts:

C++17 background

What is new in C++17?

What features are deprecated or removed in C++17?

Key features in 

C++17 

C++17 background

As you know, the C++ language is the brain child of Bjarne Stroustrup, who developed C++ in 1979. The C++ programming language is standardized by International Organization for Standardization (ISO).

The initial standardization was published in 1998, commonly referred to as C++98, and the next standardization C++03 was published in 2003, which was primarily a bug fix release with just one language feature for value initialization. In August 2011, the C++11 standard was published with several additions to the core language, including several significant interesting changes to the Standard Template Library (STL); C++11 basically replaced the C++03 standard. C++14 was published in December, 2014 with some new features, and later, the C++17 standard was published on July 31, 2017.  

At the time of writing this book, C++17 is the latest revision of the ISO/IEC standard for the C++ programming language.

This chapter requires a compiler that supports C++17 features: gcc version 7 or later. As gcc version 7 is the latest version at the time of writing this book, I'll be using gcc version 7.1.0 in this chapter.

In case you haven't installed g++ 7 that supports C++17 features, you can install it with the following commands:sudo add-apt-repository ppa:jonathonf/gcc-7.1  sudo apt-get update  sudo apt-get install gcc-7 g++-7

What's new in C++17?

The complete list of C++17 features can be found at http://en.cppreference.com/w/cpp/compiler_support#C.2B.2B17_features.

To give a high-level idea, the following are some of the new C++17 features:

New auto rules for direct-list-initialization

static_assert

with no messages

Nested namespace definition

Inline variables

Attributes for namespaces and enumerators

C++ exceptions specifications are part of the type system

Improved lambda capabilities that give performance benefits on servers

NUMA architecture

Using attribute namespaces

Dynamic memory allocation for over-aligned data

Template argument deduction for class templates

Non-type template parameters with auto type

Guaranteed copy elision

New specifications for inheriting constructors

Direct-list-initialization of enumerations

Stricter expression evaluation order

shared_mutex

 

String conversions

Otherwise, there are many new interesting features that were added to the core C++ language: STL, lambadas, and so on. The new features give a facelift to C++, and starting from C++17, as a C++ developer, you will feel that you are working in a modern programming language, such as Java or C#.

What features are deprecated or removed in C++17?

The following features are now removed in C++17:

The

register

keyword was deprecated in C++11 and got removed in C++17

The 

++

 operator for

bool

 was deprecated

in C++98 and got removed in C++17

The dynamic exception specifications were deprecated in C++11 and and got removed in

C++17

Key features in C++17

Let's explore the following C++17 key features one by one in the following sections:

Easier nested namespace

New rules for type detection from the braced initializer list

Simplified

static_assert

std::invoke

Structured binding

The

if

and

switch

local-scoped variables

Template type auto-detection for class templates

Inline variables

The std::invoke( ) method

The std::invoke() method can be used to call functions, function pointers, and member pointers with the same syntax:

#include <iostream>#include <functional>using namespace std;void globalFunction( ) { cout << "globalFunction ..." << endl;}class MyClass { public: void memberFunction ( int data ) { std::cout << "\nMyClass memberFunction ..." << std::endl; } static void staticFunction ( int data ) { std::cout << "MyClass staticFunction ..." << std::endl; }};int main ( ) { MyClass obj; std::invoke ( &MyClass::memberFunction, obj, 100 ); std::invoke ( &MyClass::staticFunction, 200 ); std::invoke ( globalFunction ); return 0;}

The preceding code can be compiled and the output can be viewed with the following commands:

g++-7 main.cpp -std=c++17

./a.out

The output of the preceding program is as follows:

MyClass memberFunction ...

MyClass staticFunction ...

globalFunction ...

The std::invoke( ) method is a template function that helps you seamlessly invoke callable objects, both built-in and user-defined.

Summary

In this chapter, you got to know interesting new features introduced in C++17. You learned the super simpleC++17nested namespace syntax. You also learned datatype detection with a braced initializer list and the new rule imposed in the C++17standard.

You also noticed that static_assertcan be done without assert failure messages. Also, usingstd::invoke(), you can now invoke global functions, function pointers, member functions, and static class member functions. And, using structured binding, you could now initialize multiple variables with a return value.

You also learned that theifandswitchstatements can have a local-scoped variable right before theifcondition andswitchstatements. You learned about auto type detection of class templates. Lastly, you used inlinevariables.

There are many more C++17 features, but this chapter attempts to cover the most useful features that might be required for most of the developers.  In the next chapter, you will be learning about the Standard Template Library.

Standard Template Library

This chapter will cover the following topics:

STL overview

STL architecture

Containers

Iterators

Algorithms

Functors

STL containers

Sequence

Associative

Unordered

Adaptors

Let's look into the STL topics one by one in the following sections.

The Standard Template Library architecture

The C++ Standard Template Library (STL) offers ready-made generic containers, algorithms that can be applied to the containers, and iterators to navigate the containers. The STL is implemented with C++ templates, and templates allow generic programming in C++.

The STL encourages a C++ developer to focus on the task at hand by freeing up the developer from writing low-level data structures and algorithms. The STL is a time-tested library that allows rapid application development.  

The STL is an interesting piece of work and architecture. Its secret formula is compile-time polymorphism. To get better performance, the STL avoids dynamic polymorphism, saying goodbye to virtual functions. Broadly, the STL has the following four components:

Algorithms

Functors

Iterators

Containers

The STL architecture stitches all the aforementioned four components together. It has many commonly used algorithms with performance guarantees. The interesting part about STL algorithms is that they work seamlessly without any knowledge about the containers that hold the data. This is made possible due to the iterators that offer high-level traversal APIs, which completely abstracts the underlying data structure used within a container. The STL makes use of operator overloading quite extensively. Let's understand the major components of STL one by one to get a good grasp of the STL conceptually.

Algorithms

The STL algorithms are powered by C++ templates; hence, the same algorithm works irrespective of what data type it deals with or independently of how the data is organized by a container. Interestingly, the STL algorithms are generic enough to support built-in and user-defined data types using templates. As a matter of fact, the algorithms interact with the containers via iterators. Hence, what matters to the algorithms is the iterator supported by the container. Having said that, the performance of an algorithm depends on the underlying data structure used within a container. Hence, certain algorithms work only on selective containers, as each algorithm supported by the STL expects a certain type of iterator.

Iterators

An iterator is a design pattern, but interestingly, the STL work started much beforeGang of Four published their design patterns-related work to the software community. Iterators themselves are objects that allow traversing the containers to access, modify, and manipulate the data stored in the containers. Iterators do this so magically that we don't realize or need to know where and how the data is stored and retrieved.

The following image visually represents an iterator:

From the preceding image, you can understand that every iterator supports the begin() API, which returns the first element position, and the end() API returns one position past the last element in the container.

The STL broadly supports the following five types of iterators:

Input iterators

Output iterators

Forward iterators

Bidirectional iterators 

Random-access iterators