How to reduce the capacity of a std::vector

With C++11, you can call the member function shrink_to_fit(). The draft standard section 23.2.6.2 says: shrink_to_fit is a non-binding request to reduce capacity() to size(). [Note: The request is non-binding to allow latitude for implementation-specific optimizations. —end note]

Benefits of using reference_wrapper instead of raw pointer in containers?

I don’t think there is any technical difference. Reference wrapper provides basic pointer functionality, including the ability to change the target dynamically. One benefit is that it demonstrates intent. It tells people who read the code that “whoever” has the variable, isn’t actually controlling its lifespan. The user hasn’t forgotten to delete or new anything, … Read more

Why does an empty vector call the value type’s default constructor?

Because you’re explicitly passing an initial size, which calls a constructor that has another parameter whose default value is s(). Just leave out the (0) (i.e. std::vector<s> v;) and it won’t happen. For completeness, the Standard 23.2.4-2 defines the constructor you’re calling as:     explicit vector(size_type n, const T& value =T(),                                     const Allocator& = Allocator()); Aside … Read more

C++ template function compiles in header but not implementation

The problem you’re having is that the compiler doesn’t know which versions of your template to instantiate. When you move the implementation of your function to x.cpp it is in a different translation unit from main.cpp, and main.cpp can’t link to a particular instantiation because it doesn’t exist in that context. This is a well-known … Read more

What is the past-the-end iterator in STL C++?

The functions begin() and end() define a half open range([begin, end)), which means: The range includes first element but excludes the last element. Hence, the name past the end. The advantage of an half open range is: It avoids special handling for empty ranges. For empty ranges, begin() is equal to end() . It makes … Read more

Does boost have a datatype for set operations that is simpler than the STL?

Nope. But I here is how to clean it up. First, rewrite iterator based functions as ranged based functions. This halves your boilerplate. Second, have them return container builders rather than take insert iterators: this gives you efficient assignment syntax. Third, and probably too far, write them as named operators. The final result is you … Read more

C++ deque: when iterators are invalidated

push_back() and push_front() are defined in terms of insert(). Similarly, pop_back() and pop_front() are defined in terms of erase(). Here’s what the C++03 standard says about iterator invalidation for insert() (23.2.1.3/1): An insert in the middle of the deque invalidates all the iterators and references to elements of the deque. An insert at either end … Read more

STL allows duplicate pairs?

The second insert with the same key is a no-op. It simply returns an iterator pointing to the existing element. std::map::insert() has a return value, which you should check. It is of type std::pair<iterator,bool>. The second element of the pair tells you whether the element has been inserted, or whether there was already an existing … Read more