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]

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

Is calling std::vector::size() as fast as reading a variable?

Interesting question. So, what’s going to happened ? Well if you debug with gdb you’ll see something like 3 member variables (names are not accurate): _M_begin: pointer to the first element of the dynamic array _M_end: pointer one past the last element of the dynamic array _M_capacity: pointer one past the last element that could … Read more

In a “i < vector.size()" loop condition, is size() called each iteration?

In theory, it is called each time, since a for loop: for(initialization; condition; increment) body; is expanded to something like { initialization; while(condition) { body; increment; } } (notice the curly braces, because initialization is already in an inner scope) In practice, if the compiler understands that a piece of your condition is invariant through … Read more

How do you remove elements from a std::vector while iterating? [duplicate]

The erase() method returns a new (valid) iterator that points to the next element after the deleted one. You can use this iterator to continue with the loop: std::vector<std::string>::iterator iter; for (iter = m_vPaths.begin(); iter != m_vPaths.end(); ) { if (::DeleteFile(iter->c_str())) iter = m_vPaths.erase(iter); else ++iter; }

How to compare two vectors for equality element by element?

Your code (vector1 == vector2) is correct C++ syntax. There is an == operator for vectors. If you want to compare short vector with a portion of a longer vector, you can use theequal() operator for vectors. (documentation here) Here’s an example: using namespace std; if( equal(vector1.begin(), vector1.end(), vector2.begin()) ) DoSomething();

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

Proper way of transferring ownership of a std::vector< std::unique_ptr< int> > to a class being constructed

std::unique_ptr<T> is a non-copyable but movable type. Having a move-only type in a std:vector<T> make the std::vector<T> move-only, too. To have the compiler automatically move objects, you need to have an r-value for move-construction or move-assignment. Within your constructor the object vecOfIntPtrsOwnedByCaller is an l-value, although one which, despite its name, already owns the pointed … Read more