Pregunta

I'm using Visual C++ 2008. I can't use pop_back because it became a member function of the string class in C++11.

What could I use instead since I can't use pop_back?

¿Fue útil?

Solución

You can use std::string::erase, which I personally find a little more clear semantically than the resize alternative:

if (!s.empty())
  s.erase (s.size()-1);

Otros consejos

You can use std::string::resize:

if (!s.empty())
  s.resize(s.size()-1);

I didn't quite understand your question but if you want to append something to a std::string you can use

std::string::append

and if you want to "shrink" it you can use

std::string::resize

There are at least two possibilites. Either to use member function erase or member function resize. for example

s.erase( s.size() - 1 );

or

s.resize( s.size() - 1 );

If you do not want to use push_back then you can substitute it for operator + or member function append.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top