在C,我们能够做到这一点的指针:转移在C++字符串
char *str;
scanf("%s", str); // lets say that we enter "Hello"
str = str + 2;
printf("%s", str); // and the answer would be "llo"
有没有这样的事情在C++处理字符串数据类型在O(1)的时间?
编辑:这是给我这个错误 - >
error: invalid operands to binary expression ('string' (aka 'basic_string<char,
char_traits<char>, allocator<char> >') and 'int')
str = str + 1;
EDIT2:我跑这个代码,并得到了上面的错误 - >
string str = "Hello";
str = str + 1;
cout << str << endl;
相同的代码会在C++中也有未定义的行为。 –
@GAURANGVYAS是的,我一上传问题就意识到了。我想用字符串数据类型来做到这一点。 –
@NeilButterworth不,字符串数据类型。对不起,我编辑了这个问题。 –