Delete All Items from a C++ Std::Vector

Delete all items from a c++ std::vector

I think you should use std::vector::clear:

vec.clear();

EDIT:

Doesn't clear destruct the elements
held by the vector?

Yes it does. It calls the destructor of every element in the vector before returning the memory. That depends on what "elements" you are storing in the vector. In the following example, I am storing the objects them selves inside the vector:

class myclass
{
public:
~myclass()
{

}
...
};

std::vector<myclass> myvector;
...
myvector.clear(); // calling clear will do the following:
// 1) invoke the deconstrutor for every myclass
// 2) size == 0 (the vector contained the actual objects).

If you want to share objects between different containers for example, you could store pointers to them. In this case, when clear is called, only pointers memory is released, the actual objects are not touched:

std::vector<myclass*> myvector;
...
myvector.clear(); // calling clear will do:
// 1) ---------------
// 2) size == 0 (the vector contained "pointers" not the actual objects).

For the question in the comment, I think getVector() is defined like this:

std::vector<myclass> getVector();

Maybe you want to return a reference:

// vector.getVector().clear() clears m_vector in this case
std::vector<myclass>& getVector();

Remove ith item from a C++ std::vector

pList.erase(pList.begin()+i);

To remove element with index i.

elegant way to remove all elements of a vector that are contained in another vector?

This one does it in O(N+M), assuming both arrays are sorted.

  auto ib = std::begin(two);
auto iter = std::remove_if (
std::begin(one), std::end(one),
[&ib](int x) -> bool {
while (ib != std::end(two) && *ib < x) ++ib;
return (ib != std::end(two) && *ib == x);
});

C++ delete elements in vector

If you simply want to erase everything off the vector, use:
http://www.cplusplus.com/reference/vector/vector/erase/

It is the erase function for vectors

// erase the first 3 elements:
newcustomer.erase (newcustomer.begin(),newcustomer.begin()+3);

How to correctly deallocate or delete a c++ vector?

The only way to really get rid off unused memory in a std::vector<> pre C++11 is to swap it with an empty vector: vector<int>().swap(myvec). In C++11 you have a member function shrink_to_fit which often is implemented as the swap idiom just mentioned.

Removing a known subset of items from a c++ vector

Since the ids in idsToRemove are known to be in items and in the same order, you can use a couple of iterators into items to keep track of the current compare element, the current destination, and walk thru idsToRemove and items, comparing both elements, moving the ones that you want to keep. At the end of that process, resize items to be the new smaller size.

Delete elements in a std::vectorstd::string which matches with the characters in another given std::string

The *it has a type of char not std::string. This is what the compiler complaining about. Therefore you need to pass a std::string to the std::find as follows.

auto toErase = std::find(alphabets.begin(), alphabets.end(), std::string{ *it });
// ^^^^^^^^^^^^^^^^^^

Here is a demo.


Also, note the followings:

  • You can change the std::vector<std::string> alphabets to a
    std::vector<char> alphabets or even a single std::string as your
    alphabets containes/ represents chars as strings. In the case of std::strings (i.e. alphabets), the std::basic_string::find
    is more appropriate to use, rather than having more general std::find at first place.
  • For vector erase, you could use erase–remove
    idiom, or
    since C++20, using non-member function of std::vector itself, so
    called
    std::erase_if.

Delete a range elements in a vector in C++

As you say the size of the vector changes, also note the indices change too so you're not removing the elements you think you are. May I suggest sorting the index vector (std::sort), and then removing from the largest index down. That way the previous indices are not invalidated by the removal of the ones before.



Related Topics



Leave a reply



Submit