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 astd::vector<char> alphabets
or even a singlestd::string
as youralphabets
containes/ representschar
s as strings. In the case ofstd::string
s (i.e.alphabets
), thestd::basic_string::find
is more appropriate to use, rather than having more generalstd::find
at first place. - For vector erase, you could use erase–remove
idiom, or
since C++20, using non-member function ofstd::vector
itself, so
calledstd::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
How to Fix: /Usr/Lib/Libstdc++.So.6: Version 'Glibcxx_3.4.15' Not Found
Compile Lua Code, Store Bytecode Then Load and Execute It
C++, How to Call a Constructor Directly, Without New
Why Not Just Use Random_Device
Cygwin Make Bash Command Not Found
Where Does One Get the "Sys/Socket.H" Header/Source File
C++ How to Allocate Memory Dynamically on Stack
"Apientry _Twinmain" and "Winapi Winmain" Difference
Counting the Number of Lines in a Text File
Sqlite3_Exec() Callback Function Clarification
Why Is Std::Vector::Operator[] 5 to 10 Times Faster Than Std::Vector::At()
C++ Linking Error After Upgrading to MAC Os X 10.9/Xcode 5.0.1
Gsl::Not_Null<T*> VS. Std::Reference_Wrapper<T> VS. T&
Getting a Directory Name from a Filename
How to Use Memcpy in C++ to Copy Classes That Have No Pointers or Virtual Functions
C++: Wrapping Vector<Char> with Istream
Is There Any Advantage to Using Pow(X,2) Instead of X*X, with X Double