randydandy
randydandy

Reputation: 45

std::string erase to delete last letter

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);

Upvotes: 4

Views: 110

Answers (1)

scohe001
scohe001

Reputation: 15446

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

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

Upvotes: 10

Related Questions