Mastering C++ Programming
eBook - ePub

Mastering C++ Programming

  1. 384 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mastering C++ Programming

About this book

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-freeWho This Book Is ForThis 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 DetailC++ 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 approachThis 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++.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

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 before
Gang 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
The container implements the iterator to let us easily retrieve and manipulate the data, without delving much into the technical details of a container.
The following table explains each of the five iterators:
The type of iterator
Description
Input iterator
  • It is used to read from the pointed element
  • It is valid for single-time navigation, and once it reaches the end of the container, the iterator will be invalidated
  • It supports pre- and post-increment operators
  • It does not support decrement operators
  • It supports dereferencing
  • It supports the == and != operators to compare with the other iterators
  • The istream_iterator iterator is an input iterator
  • All the containers support this iterator
Output iterator
  • It is used to modify the pointed element
  • It is valid for single-time navigation, and once it reaches the end of the container, the iterator will be invalidated
  • It supports pre- and post-increment operators
  • It does not support decrement operators
  • It supports dereferencing
  • It doesn't support the == and != operators
  • The ostream_iterator, back_inserter, front_inserter iterators are examples of output iterators
  • All the containers support this iterator
Forward iterator
  • It supports the input iterator and out...

Table of contents

  1. Title Page
  2. Copyright
  3. Credits
  4. About the Author
  5. About the Reviewer
  6. www.PacktPub.com
  7. Customer Feedback
  8. Dedication
  9. Preface
  10. C++17 Features
  11. Standard Template Library
  12. Template Programming
  13. Smart Pointers
  14. Developing GUI Applications in C++
  15. Multithreaded Programming and Inter-Process Communication
  16. Test-Driven Development
  17. Behavior-Driven Development
  18. Debugging Techniques
  19. Code Smells and Clean Code Practices

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Mastering C++ Programming by Jeganathan Swaminathan in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming. We have over one million books available in our catalogue for you to explore.