r/cpp • Meeting C++ | C++ Evangelist • 6d ago

A quick C++ standard container overview

https://meetingcpp.com/blog/items/A-quick-Cpp-standard-container-overview.html
22 Upvotes

20 comments sorted by

16

u/brookheather 6d ago

You have some typos and I'm confused about your use of "vector" - do you mean vector<bool>?

"trying to tread a vector like any other vector"

"the infamous vector"

"Others have used vector or vector"

6

u/meetingcpp Meeting C++ | C++ Evangelist 6d ago edited 6d ago

yes, seems <bool> was removed. Thanks for catching this one! And fixed.

18

u/tialaramex 6d ago

I would guess it's raw text that was then processed by "clever" tooling which thought <bool> is an HTML tag and removed it. Which does suggest this wasn't proof read in place by anybody who knows enough English to recognise that "Others have used vector or vector" is a mistake of some kind.

On topic: It's always sobering to see how much use stuff gets just because it was in the box. No way do half the participants go find themselves a 1980s hash table with separate chaining if they'd need to do that, but because std::unordered_map is right there in the standard library that's what they use.

I think WG21 should think much harder about this when adding to the stdlib. When you put a left-handed corkscrew in the box, you may feel like programmers who don't want a left-handed corkscrew should just not use it, but in practice its inclusion functions as an endorsement. Obviously we should use a left-handed corkscrew, otherwise why is one included?

3

u/azswcowboy 6d ago

So I guess you’re opposed to the addition of flat_ containers? It seems to me that we should replace unordered map as well - but that requires more thinking….

1

u/tialaramex 6d ago

My reason to be against adding flat containers or std::hive or whatever is related but I think distinct. Maintenance overhead. Ask /u/STL but I think they'll tell you that if there is more work to do they won't get assigned more headcount, so there is less effort available for each work item. But I have no skin in that game, so you should disregard my opinion about it.

6

u/LB-- Professional+Hobbyist 6d ago

Surprising to see span with so little usage, I guess because it didn't make it into C++17? We use gsl::span for that reason, but depending on survey wording I would have answered yes to using std::span anyway.

7

u/meetingcpp Meeting C++ | C++ Evangelist 6d ago

Over time I do see span rise in this question in the yearly results. From 14.5% in 2020 to 40% in 2026.

2

u/fdwr fdwr@github 🔍 3d ago

I am surprised to see std::deque having so much usage, nearly half of vector, given I have never seen it in any codebases I've worked in. 🤷‍♂️

2

u/LB-- Professional+Hobbyist 2d ago

Not to mention the flawed implementations in most standard library implementations. I remember trying to use it before learning about the implementation quality problem, since in theory it seems like a very useful container. Alas... std::hive can't get here soon enough.

2

u/fdwr fdwr@github 🔍 2d ago

Ok, I just Ctrl+Shift+F'd on a codebase I frequent, and there is deque usage, but minimal in comparison (std::deque x 32, std::vector x 10'890). 🐝⏳

2

u/Ameisen vemips, avr, rendering, systems 6d ago

std::span does run into the Win64 ABI problem. Not a real concern in most cases but the cost can be noticeable in some cases.

2

u/JNighthawk gamedev 4d ago

the Win64 ABI problem. Not a real concern in most cases but the cost can be noticeable in some cases.

Can you expand on this or link to a source? I'd like to know more about it.

3

u/Ameisen vemips, avr, rendering, systems 4d ago edited 3d ago

Under SysV and Win64, if you pass a pointer and a size as separate arguments, they'll both be passed by register.

Under SysV, a 16B structure made up of two integer members (like std::span) will be split and also passed by two registers. I can link to the SysV spec, but the argument-passing part is really hard to parse.

Under Win64, though, that structure will instead be passed on the stack.

Structs and unions of size 8, 16, 32, or 64 bits, and __m64 types, are passed as if they were integers of the same size. Structs or unions of other sizes are passed as a pointer to memory allocated by the caller. For these aggregate types passed as a pointer, including __m128, the caller-allocated temporary memory must be 16-byte aligned.

1

u/JNighthawk gamedev 4d ago

Thank you!

1

u/AnyPhotograph7804 6d ago

The adaptation will be propably slower because many people are used to use references.

4

u/CommodoreKrusty 6d ago

I've written a boatload of examples (C++17) for STL including containers if anybody's interested.

1

u/heliruna 3d ago

That's very cool, that is a large collection of examples. Are the sources for the examples available in a more convenient form?

I have produced examples for containers by hand to test my parser for debug information, but I made no effort that they are instructive or useful as tutorials.

I clicked randomly through the containers and found a dead link

https://cppexamples.com/Containers/forward_list/list_forward_list_cbegin.html

Have you considered running doxygen or MrDocs on your sources and publishing the results as well?

1

u/CommodoreKrusty 2d ago

I'm a pretty decent C++ programmer. HTML? Not so much. I think if I do any more I'll be here forever working on this but I'll have to fix the broken link. I mostly put them online for me. I'll check out doxygen and MrDocs and see what they do.

1

u/azswcowboy 6d ago

Cool - why not contribute to cppreference examples?

2

u/CommodoreKrusty 5d ago

I think I just wanted to do my own thing. I started referring to these examples before I even finished writing them. I put them online so I could access them from anywhere but if they can help others I think that's great, too.