Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying C++ STL Cookbook
  • Table Of Contents Toc
C++ STL Cookbook

C++ STL Cookbook - Second Edition

By : Bill Weinman
close
close
C++ STL Cookbook

C++ STL Cookbook

By: Bill Weinman

Overview of this book

C++ STL Cookbook is a comprehensive guide that provides practical solutions for mastering the latest features of the C++23 Standard Template Library (STL) through hands-on recipes. Beginning with new features in C++23, this book will help you understand the language's updated mechanics and library features, and offer insights into how they work. Unlike other books, this cookbook takes an implementation-specific, problem-solution approach that will help you overcome hurdles quickly. You'll learn core STL concepts, such as containers, algorithms, utility classes, lambda expressions, iterators, and more, through specific real-world recipes. Building on the success of the first edition, this updated guide includes a new chapter dedicated to the latest features introduced in C++23, such as improved modules, refined ranges, and coroutine-based generators. It also covers essential best practices for writing cleaner and more efficient code, including the use of coroutines, structured bindings, and std::span. Whether you're looking to deepen your understanding of the C++ STL or implement the latest features in your projects, this book provides valuable insights, clear and concise explanations and practical solutions to enhance your C++ programming skills.
Table of Contents (15 chapters)
close
close
14
Index

Initialize complex structures from file input

One strength of the input stream is its ability to parse different types of data from a text file and convert them to their corresponding fundamental types. Here's a simple technique for importing data into a container of structures using an input stream.

How to do it

In this recipe we'll take a data file and import its disparate fields into a vector of structs. The data file represents cities with their populations and map coordinates.

  • This is cities.txt, the data file we'll read:
    Las Vegas
    661903 36.1699 -115.1398
    New York City
    8850000 40.7128 -74.0060
    Berlin
    3571000 52.5200 13.4050
    Mexico City
    21900000 19.4326 -99.1332
    Sidney
    5312000 -33.8688 151.2093

    The city name is on a line by itself. The second line is population, followed by longitude and latitude. This pattern repeats for each of the five cities.

  • We'll define our filename in a constant so we can open it later...
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
C++ STL Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon