How can you erase elements from a vector while iterating?

Since C++20, there are freestanding std::erase and std::erase_if functions that work on containers and simplify things considerably: std::erase(myNumbers, number_in); // or std::erase_if(myNumbers, [&](int x) { return x == number_in; }); Prior to C++20, use the erase-remove idiom: std::vector<int>& vec = myNumbers; // use shorter name vec.erase(std::remove(vec.begin(), vec.end(), number_in), vec.end()); // or vec.erase(std::remove_if(vec.begin(), vec.end(), [&](int x) … Read more

What are the constraints on the user using STL’s parallel algorithms?

The short answer is that the element access functions (essentially the operations required by the algorithms on the various arguments; see below for details) used with algorithms using the execution policy std::execution::parallel are not allowed to cause data races or dead-locks. The element access functions used with algorithms using the execution policy std::execution::parallel_unsequenced_policy additionally can’t … Read more

C++ type suffix _t, _type or none

As @MarcoA.’s answer correctly points out, the suffix _t is largely inherited from C (and in the global namespace – reserved for POSIX). This leaves us with “no suffix” and _type. Notice that there is no namespace-scope name in std ending in _type*; all such names are members of classes and class templates (or, in … Read more

Is it reasonable to use std::basic_string as a contiguous buffer when targeting C++03?

I’d consider it quite safe to assume that std::string allocates its storage contiguously. At the present time, all known implementations of std::string allocate space contiguously. Moreover, the current draft of C++ 0x (N3000) [Edit: Warning, direct link to large PDF] requires that the space be allocated contiguously (§21.4.1/5): The char-like objects in a basic_string object … Read more

C Analog To STL

Yes, glib is a pretty good choice: it includes a lot of utilities for manipulating containers like linked lists, arrays, hash tables, etc. And there is also an object-oriented framework called GObject that you can use to make objects with signals and slots in C (albeit with rather verbose function call names like gobject_set_property, since … Read more

How to cheaply assign C-style array to std::vector?

The current std::vector doesn’t provide any capability or interface to take ownership of previously allocated storage. Presumably it would be too easy to pass a stack address in by accident, allowing more problems than it solved. If you want to avoid copying into a vector, you’ll either need to use vectors through your entire call … Read more