galois galois - 1 year ago 72
C++ Question

Treat c++ string as a pointer

How can the STL and

instead of
be used to go about jumping around a string like this (admittedly nonsensical) example?

char* s = "XXXXXXXXhello";
while (*s == 'X')

s += 2;
std::cout << ----s; //prints `hello`

Answer Source

If what you want to do is modify the object, and get rid of "h":

std::string s = "hello";
s = s.substr(1); // position = 1, length = everything (npos)
std::cout << s;  //"ello"


std::string s = "hello";
s.erase(0, 1); // position = 0, length = 1
std::cout << s;  //"ello"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download