How To Remove A Value From C++ Vectors (Value Based Remove)

Hi guys, if you are working with vectors in CPP you may require some usage of vectors. For example removing a value from vector is some of them.

In CPP vectors, there are two delete method. 

One of them erase other is remove. 

When you wanted to use "erase" you should consider "erase function only takes pointer" of the value.

For example I have a vector like this:

vector<string> blogkafem = {"h", "e", "l", "l", "o"};

If I decide to use erase function, at first I should get address of the value with pointer. For using pointers in vectors you should use begin() function. What does begin() function? It takes pointer value of the first element in the vector. After you get first element of the vector, you can discover other vectors easily.

Let's do it!

vector::<string>iterator iteratorOfVector = blogkafem.begin();

"iteratorOfVector" is a pointer. If you want to print it with "cout" you should write it like this:

cout << *iteratorOfVector << endl;

It prints "h" value! What a great!!! Hahaha..

Lets continue..

As I said about "erase" it only takes pointer. If you want to remove "e" value. You should use it like this:

blogkafem.erase(iteratorOfVector + 1);

That's why for this usage we can say, this method removes values "index" based or "pointer" based. If you want to remove content of the vector dynamically, for example in the "for" loop you may need to value based remove.

At this point! You should use "remove" function into "erase" function like this:

blogkafem.erase(std::find(blogkafem.begin(), blogkafem.end(), "h")); 

That's it!

(alternative usage : blogkafem.erase(std::remove(blogkafem.begin(), blogkafem.end(), "h"), blogkafem.end()); )

As you see, you may need more letter of code for removing an item from a dynamic array when you compare it with other languages but unfortunately this is the nature of CPP. 

Not : For using "remove" you should include algorithm:

#include <algorithm>


But "remove" function only removes value of the vector address is not removing. For example if you have vector with size 5, after removing value with "remove()" function you still will see your vector has (size: 5) same size. When we use remove into erase we remove value from vector completely and your vector size will be 4 after you size array. It is really as is scenario that we wanted!

Here is good and basic example :

#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

using namespace std;

int main() {

    vector<string> items = {"a","b","c","ç","d"};  

    int selectedItem;

    while (items.size() > 0) {

    cout << "vector size : " << items.size() << endl;

    selectedItem = rand() % items.size();

    if (selectedItem < items.size()) {

      cout << "selectedItem - index : " << selectedItem << endl;

      cout << "iterator - selected value : " << items[selectedItem] << endl;

      items.erase(find(items.begin(), items.end(), items[selectedItem]));

    }

    cout << "///////////////////////////////////////////////" << endl;

  }

}

Let's try it : http://cpp.sh/6ux7u6

I had need algorithm like these related with my study. After I coded this algorithm example, I recognize vectors and its capabilities better about removing an item from a vector. Thanks to this example I learned differences between erase and remove functions. I hope it would help you too.

Comments

Popular posts from this blog

How To Solve Class Not Found Problem in OMNeT++

PHP Yii Framwork Autocomplete Widget Code