Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Data Structures and Algorithms with the C++ STL

You're reading from   Data Structures and Algorithms with the C++ STL A guide for modern C++ practitioners

Arrow left icon
Product type Paperback
Published in Feb 2024
Publisher Packt
ISBN-13 9781835468555
Length 458 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
John Farrier John Farrier
Author Profile Icon John Farrier
John Farrier
Arrow right icon
View More author details
Toc

Table of Contents (30) Chapters Close

Preface 1. Part 1: Mastering std::vector FREE CHAPTER
2. Chapter 1: The Basics of std::vector 3. Chapter 2: Mastering Iterators with std::vector 4. Chapter 3: Mastering Memory and Allocators with std::vector 5. Chapter 4: Mastering Algorithms with std::vector 6. Chapter 5: Making a Case for std::vector 7. Part 2: Understanding STL Data Structures
8. Chapter 6: Advanced Sequence Container Usage 9. Chapter 7: Advanced Ordered Associative Container Usage 10. Chapter 8: Advanced Unordered Associative Container Usage 11. Chapter 9: Advanced Container Adaptor Usage 12. Chapter 10: Advanced Container View Usage 13. Part 3: Mastering STL Algorithms
14. Chapter 11: Fundamental Algorithms and Searching 15. Chapter 12: Manipulation and Transformation 16. Chapter 13: Numeric and Range -Based Operations 17. Chapter 14: Permutations, Partitions, and Heaps 18. Chapter 15: STL with Ranges 19. Part 4: Creating STL-Compatible Types and Algorithms
20. Chapter 16: Creating STL-Types Containers 21. Chapter 17: Creating STL -Compatible Algorithms 22. Chapter 18: Type Traits and Policies 23. Part 5: STL Data Structures and Algorithms: Under the Hood
24. Chapter 19: Exception Safety 25. Chapter 20: Thread Safety and Concurrency with the STL 26. Chapter 21: STL Interaction with Concepts and Coroutines 27. Chapter 22: Parallel Algorithms with the STL 28. Index 29. Other Books You May Enjoy

Declaring and initializing std::vector

Having established the foundational knowledge of std::vector in C++ development, it’s time to delve into its practical applications—expressly, how to declare and initialize vectors.

The essence of std::vector lies in its dynamic nature. Unlike traditional arrays with fixed sizes, vectors can grow or shrink as necessary, making them a versatile tool for developers.

Declaring a vector

The performance of std::vector stems from its design, which combines the benefits of a contiguous memory layout (such as arrays) with the flexibility of dynamic resizing. It reserves memory to hold that many elements when initialized with a specified size. But if the vector fills up and more capacity is needed, it allocates a larger memory block, transfers the existing elements, and deallocates the old memory. This dynamic resizing process is optimized to reduce overhead, ensuring that vectors remain efficient. The fusion of contiguous storage...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime