[Solved] std::vector inserting std::pair

The original poster failed to actually post the code that was causing the problem. He has since edited my post with the correct code that demonstrates the problem. The code that demonstrates the problem follows: template <class R, typename B=int> class AVectorContainer { public: AVectorContainer() {} typedef R* ptr_Type; typedef const B & const_ref_Type; typedef … Read more

[Solved] std::vector push_back results in access Violation

Dict* test = (Dict*)malloc(sizeof(Dict)); does not do what you think it does. malloc allocates a block of memory, but does not initialize it. So later on, when you call test->nodes.push_back, you’re calling into uninitialized memory. Undefined behavior. In your case, it crashes. The solution here is allocate test with new, which will initialize both test … Read more

[Solved] What variations of vector-like containers already widely established? Do I have to write my own? [closed]

Here are the ones I know of: Traditional a.k.a. plain a.k.a. C array vector unique_ptr with an array-type template argument array valarray dynarray static_vector (see also here) small_vector stable_vector There are also “variable-length arrays” and “arrays with runtime bounds”, which do not exist in C++; the former exists in C. See also this question and … Read more

[Solved] speeding vector push_back [closed]

Indeed push_back may trigger reallocation, which is a slow process. It will not do so on every single push_back though, instead it will reserve exponentially more memory each time, so explicit reserve only makes sense if you have a good approximation of resulting vector size beforehand. In other words, std::vector already takes care of what … Read more

[Solved] C++ program does nothing when executed

Vectors in c++ are dynamically sized. You can create a vector without a size argument in the constructor then push size number of elements like so: vector<int> makeGaps (int size){ vector<int> vectorOfGaps; for(int i = 0; i < size;i++){ vectorOfGaps.push_back(i); } return vectorOfGaps; } Edit: Also, as someone already pointed out in your comments, it … Read more