How to find a specific element in a vector of shared_ptr?

In my project, every time I type a letter, I create an object (shared_ptr) that I store in a vector.
What I want is to use my keyboard like a toggle, so if the letter is on the screen, delete it, if not, create an object.

To do so, I think I need an Iterator to check if the object is already in the vector, but I don’t know how to compare the new one with all these in the vector. Here’s the part of my code :

> std::vector<std::shared_ptr<OneLetter>> letters; // declare shared_ptr
> void keyPressed(int key){
> std::vector<std::shared_ptr<OneLetter>>::iterator it; 
>      for(std::vec<std::shared_ptr<OneLetter>>::iterator it = letters.begin(); it != letters.end(); ++it){
>      // here I don't know how to compare ?
>           if(*it == letters){
>                it = letters.erase(it);
>           }
>      }
> }

it is probably a lot faster to preallocate the vector and then use the key as the index for the vector.

so In the

void setup(){
letters.resize(127);// I am not sure if all the key modifiers and other kinds of letters fit here. change if you need to.
void keyPressed(int key){
if(letters[key] == nullptr){
letters[key] = make_shared<OneLetter>();
letters[key] = nullptr;

Thank’s Roy! It work’s perfectly!

1 Like