C++ Concept Visualizations

So I've been thinking of something and i thought it would be helpful to be able to see how things work in C++ instead of trying to visualize them yourself, its always helpful to have a visualization. i think it helps clear things up very well if done correctly. I wanted to try to do something like that, create diagrams and get them peer reviewed and put them up so people can learn from them. It's just an idea i had. Here is one i made for vectors, its just a basic one I'm still working on, so its just a rough draft still, but id like to know what others think of it. This doesn't explain what's going on underneath the hood of the vector itself but how you actually use a vector and what happens when you add elements to it, although creating a diagram for how vectors are implemented would be interesting, however i don't know too much about that and its quite complex so that will have to wait haha.

https://ibb.co/FXKZFCz

The image is quite large so it might be hard to read text, i would suggest just saving it to desktop and zooming in to read. Thats another point i want to address. Each one of these should be fully detailed and not be broken up into separate images, so if the image has to be massive and the user has to zoom in to see them then so be it, but i find everything on a single image is easier to comprehend, so long as its layed out correctly and explained well enough.

I would like to do this for all concepts in C++, i think it would be not only a great way for me to learn, but helpful for others as well.
Last edited on
A visualization should be something that you can look at and be able to understand the concept with minimal text/speech. Sadly, this visualization is just too much, it loses my attention as soon as I look at it.

A visualization should be as simple as possible. You're not trying to teach the entire concept, only give them a starting point in which to start thinking and learning about the concept.

What I'd expect as a visualization for vectors (in the way you seem to be showing) would be the state of the memory as the vector is actually used. For example:

       Code                 Visual
____________________ | ________________

                             ___
std::vector<int> a          |___|

                             ___
a.push_back(2)              | 2 |
                            |___|
                             ___
a.push_back(5)              | 2 |
                            | 5 |
                            |___|



A visualization should be something you look at and go, "ahh I see" rather than something to be studied itself in my opinion.
Last edited on
I like it at a high level, but images tend to oversimplify thing. You need both diagrams and text, but a good bit of text as the image can't replace that, only clarify it.

Pictures are troubling in print... take up a lot of space, unless you try to replace all the words with the image, in which case it can save space at the cost of leaving it to the student to make up the words from the image. The image above tells me that vector is a stack, and without words, that is what I would now believe. This isn't wrong (a vector IS a stack of sorts) but its not really right either.
Last edited on
Yeah, a vector isn't a C++ stdlib stack container, duh. :) A std::stack is a container adapter, usually based on std::deque. Of course each compiler implementation could use a different base container.

https://en.cppreference.com/w/cpp/container/stack

The closest analogy IMO could be considered an old school C style array that uses pointers and new/delete instead of calloc/malloc/realloc/free to be dynamically sized. Encapsulated in a C++ class to manage things with less hassle, including memory.

Memory management is one fundamental part of C/C++ programming that is not easy to understand in one easy to learn lesson.

C++11 added std::array to the containers library that is a static sized C style array on steroids, with a not very intuitive interface.

The C11 and C23 standards add several new functions that on first look are useful for managing memory: aligned_alloc (C11), free_sized (C23) and free_aligned_sized (C23).

https://en.cppreference.com/w/c/memory

@Ch1156, FYI, the term Concepts has taken on a different meaning with C++20.

https://en.cppreference.com/w/cpp/concepts

By context it is easily understood what you mean, you are talking about C++ containers, but be aware it could be confusing.

Well, the diagrams aren't really meant for beginners, or even for reference purposes, its made to fully illustrate how a C++ feature works. It isn't meant for someone to quickly look at and say, "I get it now" its meant to be studied and to fully explain in detail how that feature works and what its doing when you interact with it, which in my personal opinion and experience, its much easier to learn when things can be visualized, since I have a hard time grasping things and visualizing them in my head.

Perhaps its really most useful to people like me who find it extremely hard sometimes to understand concepts, but when they are explained in ways that I can visualize, i can understand much easier.

The diagram i created is a bit of a mess and incomplete so i need to work on that and fix it up a bit, also I'll probably lay out the vector elements like i did memory, i just stacked them on top because since a vector is a container, i think of it more like a list, so visually I had them stacked like that, but I can see how that might confuse people so i will change that. I appreciate all the feedback and advice so far! I will continue to refine this!
Last edited on
@ George..
While not always ideal, vectors can morph into a wide variety of data structures.

they can easily represent any graph using indices as if pointers (list, tree, graph etc)

they can be an array/dynamic array /multi dimensional array or matrix

they inherently perform fine as a stack.
they are poor at it but can be a queue or priority q as well thx to insert() or as-a-list above.

they inherently abstract memory management and can be used for that directly if you are not in some 'save every cpu cycle' extreme performance need, which is more and more rare today, esp now that we have finally broken the soft 3ghz cpu era limit for home/business desktops.

I think I could probably spend a full month in computer science 101 or 102 on nothing but vectors if I were teaching it. Its so rare that I use any other container, depending on whether you feel string is a container or no.

I left off that they can be a hash table and some of the other goodies as the dedicated tools are decidedly better for those uses. For everything I listed, pure c++ either lacks it or their version isn't notably better, though libraries fill in many of the gaps far better than home brew.
Last edited on
There is a difference between the theory of a stack in generic programming and std::stack. Which is what I (tried) to point out. I never said std::vector couldn't be used for a variety of tasks.

This discussion is geared towards basic elementary understanding of "what is a std::vector?".

I could very easily have blathered on a lot more about std::vector and its uses. And misuses.

The memory allocation for elements of a std::vector according to the standard is contiguous, the same as a C-style vector. Other containers have their elements scattered around in memory, they are not contiguous.

Yes, using a std::vector as the base class for the adapter for a custom designed stack is entirely doable. If'n I ever needed to "roll my own" stack solution. But are other C++ containers better suited to the task? Yes, there are.

Right or wrong, I'm of the opinion if C++ provides something, such as std::stack, I am not about to manually mash something together. Unless I have very specific needs the C++ stdlib std::stack doesn't provide.

Even if earlier C++ versions required creating my own stuff. I might as well just code in machine language then.

Each C++ stdlib container has its uses, and deficits. Memory layout, random access of elements, speed of adding and removing elements, etc. There is no one container that fits every need. Hence the decision by the standards committee to create a variety of containers.

I myself use std::vector almost exclusively, even in multi-dimensional usages. 2nd most used, around 2-3%, is std::list. With what I do I don't need sets or maps or stacks or the other containers. Maybe one day.

C++26 will add a new container: std::inplace_vector.

https://en.cppreference.com/w/cpp/container/inplace_vector

I can see some definite advantages to having a specified number of max elements. Though the syntax for construction is as clunky as for a std::array IMO. Crufting a multi-dimensional std::array is a bit perverse, setting each dimension is in reverse order of what to expect from a C-style MD array or a MD std::vector. I'd bet the inplace_vector has the same or similar problems.

I haven't yet really looked at C++23 std::mdspan beyond knowing it exists. There are very few examples of usage available on the interwebz. The one example I could find makes using it rather complicated with extents, etc.

https://www.studyplan.dev/pro-cpp/mdspan
Last edited on
create diagrams and get them peer reviewed and put them up so people can learn from them.

2 things to note about the diagram (my opinion):

1. I've seen Rube Goldberg inventions that are far less cluttered and complicated than the std::vector diagram. Too much information that overwhelms the viewer. (https://duckduckgo.com/?t=ffab&q=rube+goldberg+inventions&iax=images&ia=images)

2. The picture itself is too small. When "blown up" the image, and especially the text, gets blurry and very hard to read.

This single picture could (and likely should) be segmented out into multiple images. With each image illustrating a core idea that segues into the next image that provides more information. A slideshow.

the diagrams aren't really meant for beginners

Then the usefulness of the image is not all that effective for anyone else other than you, the creator. IMO.

You wanted peer review.....

Overall a good effort for a "from scratch" start, your idea just needs more refinement to be a useful teaching tool. For beginners and above. Overall "there's too many notes."

https://www.youtube.com/watch?v=H6_eqxh-Qok

Cicero wrote:
When you wish to instruct, be brief; that men's [children's] minds take in quickly what you say, learn its lesson, and retain it faithfully. Every word that is unnecessary only pours over the side of a brimming mind.

Cicero could have been a wee bit more brief with that. :Þ

More on teaching: https://www.teachthought.com/pedagogy/great-best-quotes-about-teaching/
Last edited on
std::stack is just a wrapper around another class - by default std::deque. But the underlying used class could be std::vector, std::list (or other specified class that fulfils the requirements) that is specified as part of the stack constructor.
The same applies to std::queue with the additional requirement that the container must support pop_front() which excludes std::vector (default is std::deque with std::list being the other choice).
I will see if i can figure out how to make these more digestible while still retaining the original concept idea. Thank you all for the feedback!
Registered users can post here. Sign in or register to post.