Hey guys, i have 2 questions to ask. Firstly ,i would like to comfirm, does using clear()function on vector and multimap , helps to destroy the elements in the vector and deallocate memory space since the elements inside it was destroyed? If it isnt, what functions can i use to make sure memory allocated to it will be deallocated.
How about just a normal string vector which does not contain pointers?
For example
1 2 3 4 5 6 7 8 9 10 11 12
vector<string> HeaderVec2;
vector<string>::iterator i;
HeaderVec2.push_back("Name");
HeaderVec2.push_back("Randy");
for (i = HeaderVec2.begin(); i != HeaderVec2.end(); ++i)
{
delete (*i);
}
HeaderVec2.clear();
When i tried doing this , i got an error " error: type ‘struct std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int>’ argument given to ‘delete’, expected pointer ". Is this due to the fact that, i just normally inserted values into my vector without using pointers?
As far as I know there is no way to reduce the memory allocated to a std::vector. What you can do is either create the vector using new so you can delete it when you want to return the memory or put the vector in its own scope:
1 2 3 4 5 6 7 8 9 10 11 12 13
int main()
{
// do some stuff
{ // introduce a new scope
std::vector<int> v;
// do stuff with vector
} // end of scope, vector is destroyed returning memory
// do some more stuff
}