Search code examples
c++vectorstackerase

C++ Deleting a specfic value in a vector without knowing location


if (find(visitable.begin(), visitable.end(), ourstack.returnTop())) { ... }

I want to determine whether the top character in stack ourstack can be found in the vector visitable. If yes, I want this character to be deleted from visitable.

How would I code that? I know vectors use erase, but that requires the specific location of that character (which I don't know).

This is for my maze-path-finding assignment.

Also, my returnTop is giving me an error: class "std.stack<char..." has no member returnTop. I declared #include in the top of my program. What's happening here?

Thanks in advance!


Solution

  • If you are using find, then you already know the location of the character. find returns an iterator to the position where the character is found, or to the value used as end if it cannot find it.

    vector<?>::const_iterator iter =
        find(visitable.begin(), visitable.end(), ourstack.top());
    if( iter != visitable.end() )
    {
        visitable.erase( iter );
    }
    

    As for stack, the function you are looking for is top(). The standard C++ library does not use camelCased identifiers, that looks more like a Java or C# thing.