The rules to the C++ standard library are mainly about containers, strings, and iostreams.
Curiously, there is no section on the standard template library (STL) algorithms in this chapter. Curiously, because there is a proverb in the C++ community: If you write an explicit loop, you don’t know the algorithms of the STL. Anyway. Only for completeness, let me start with the first three rules which do provide not much beef.
SL.1: Use libraries wherever possible, because reinventing the wheel is a bad idea. Additionally, you benefit from the work of others. This means you use already tested and well-defined functionality. This holds, in particular, true if you SL.2: Prefer the standard library to other libraries. Imagine, for example, you hire someone. The benefit is that he already knows the library, and you don’t have to teach him your libraries. You save a lot of money and time. I once had a customer who named his infrastructure namespace std. Of course, if you want to have a lot of fun, do it. If not: SL.3: Do not add non-standard entities to namespace
The next rules to STL containers are more concrete.
The first rule is quite easy to argue.
I assume you know a std::vector. One of the significant advantages of a std::vector to a C array is that the std::vector automatically manages its memory. Of course, that holds true for all further containers of the Standard Template Library. But now, let’s look closer at the automatic memory management of std::vector.
To spare typing, I wrote the small function showInfo. This function returns for a vector its size and its capacity. The size of a vector is its number of elements, and the capacity of a container is the number of elements a vector can hold without an additional memory allocation. Therefore, the capacity of a vector has at least to be as big as its size. You can adjust the size of a vector with its method resize and the capacity of a container with its method reserve.
But, back to the program from top to bottom. I create (line 1) an empty vector. Afterward, the program displays (line 2) the maximum number of elements a vector can have. After each operation, I return their size and capacity. That holds for the initialization of the vector (line 3), for the addition of four new elements (line 4), the resizing of the containers to 30 elements (line 5), and the reserving of additional memory for at least 1000 elements (line 6). With C++11, you can shrink with the method shrink_to_fit (line 7) the vector’s capacity to its size.
Before I present the program’s output on Linux, let me make a few remarks.
- Adjusting the size and capacity of the container is done automatically. I haven’t used any memory operations like new and dele
- By using the method vec.resize(n) the vector vec will get new default-initialized elements if n > cont.size() holds.
- By using the method vec.reserve(n) the container vec will get new memory for at least n elements if n > cont.capacity() holds.
- The call shrink_to_fit is non-binding. That means the C++ runtime has not had to adjust the capacity of a container to its size. But, using the method shrink_to_fit with GCC, clang, or cl.exe always freed the unnecessary memory.
Okay, but what is the difference between a C array and a C++ array?
std::array combines the best of two worlds. On the one hand, std::array has the size and efficiency of a C-array; on the other hand, std::array has the interface of a std::vector.
My small program compares the memory efficiency of a C array, a C++ array (std::array), and a std::vector.
The C array (line 1) and the C++ array (line 2) take 40 bytes. That is precisely sizeof(int) * 10. In contrast, the std::vector needs additional 24 bytes (line 3) to manage its data on the heap.
This was the C part of a std::array, but the std::array supports the interface of a std::vector. This means, in particular, that std::array knows its size; therefore, error-prone interfaces such as the following one are a heavy code smell.
When you use a C array as a function argument, you remove almost all type information and pass it as a pointer to its first argument. This is extremely error-prone because you must also provide the number of elements. If your function accepts a std::array<int, 100>, this will not hold.
You can use a template if the function good is not generic enough.
Because a std::array knows its size, you can ask for it in line 1.
The following two rules for containers are pretty interesting. In the next post, I will give an answer to the question: When to use which container?
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, and Marco Parri Empoli.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,