免责声明——我来自非常严格的 C 语言背景。
如何使用 STL 和 std::string
而不是 char*
来绕过像这样(公认的荒谬)示例的字符串?
const char* s = "XXXXXXXXhello";
while (*s == 'X')
s++;
s += 2;
std::cout << --(--s); //prints `hello`
最佳答案
如果你想做的是修改对象,去掉“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"
关于c++ - 将 C++ 字符串视为指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35532552/