+ 3
"Also when I'm trying to remove 6 from vector, why do I get
2, 4, 8, 9, 9 instead of
2, 4, 8, 9"
Still, as quoted from https://en.cppreference.com/w/cpp/algorithm/remove
"Removing is done by shifting (by means of move assignment) the elements in the range in such a way that the elements that are not to be removed appear in the beginning of the range.
Relative order of the elements that remain is preserved and the physical size of the container is unchanged.
Iterators pointing to an element between the new logical end and the physical end of the range are still dereferenceable, but the elements themselves have unspecified values (as per MoveAssignable post-condition)."
First you have 2, 4, 6, 8, 9
6 is removed, and other elements whose value not equal to 6 are shifted to the left, towards beginning of the range. Value 8 and 9 are shifted to the left, thus you have ...
2, 4, 8, 9, 9
8 takes the slot of 6, 9 takes the slot of 8.
*** To be continued ***
+ 5
Manav Roy I believe this will help you understand.
https://www.geeksforgeeks.org/difference-between-stdremove-and-vectorerase-for-vectors/
Look at the examples provided
pend = std::remove(vec.begin(), vec.end(), 9);
// std :: vector :: erase function call
// erase the last element of vector
ve.erase(ve.begin(), ve.end() 1);
+ 4
"Is std:: remove suppose to remove element from a vector?"
Yes, but actually it's not only on vectors, any container supporting iterators will be usable, cause this function works by means of forward iterator
"If so,Then in the below code,Why 9 is not removing?"
Quoted from https://en.cppreference.com/w/cpp/algorithm/remove ...
"A call to remove() is typically followed by a call to a container's erase() method, which erases the unspecified values and reduces the physical size of the container to match its new logical size."
std::remove returns an iterator which points to past-the-end of the new range. That is, past-the-end of the new range which contains preserved (unremoved) elements.
+ 3
The last 9 you see in there was because you didn't follow the basic guide ...
"A call to remove() is typically followed by a call to a container's erase() method, which erases the unspecified values and reduces the physical size of the container to match its new logical size."
You're supposed to store std::remove() return value (a forward iterator) and use the returned iterator to erase undesired part of the range, which contains unspecified values (data) - that you unlikely want.
After the erasure of the unwanted segment of the range, the container should have updated its physical size accordingly, then it is safe to display its contents using iteration from begin() to end() again. Assuming the vector instance was named <vec>, and the iterator returned by std::remove was named <fwd_iterator> ...
vec.erase( fwd_iterator, vec.end() );
As you could see in the example in the link to cppreference page above
Hth, cmiiw