std::string erase to delete last letter

Viewed 94

I want to take some input for a game, and delete the last letter from the string after pressing backspace. I'm not sure if I should do text.end -1, or +1 to end to do so:

if (GetAsyncKeyState(VK_BACK))
    text.erase(text.end - 1, text.end);
1 Answers

std::string actually has a pop_back() method! So you can do:

if (GetAsyncKeyState(VK_BACK) && !text.empty()) { text.pop_back(); }
Related