Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::string erase to delete last letter

Tags:

c++

string

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);
like image 567
randydandy Avatar asked Dec 23 '22 04:12

randydandy


1 Answers

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

if (GetAsyncKeyState(VK_BACK) && !text.empty()) { text.pop_back(); }
like image 144
scohe001 Avatar answered Dec 28 '22 06:12

scohe001