在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不,字符串數據類型。對不起,我編輯了這個問題。 –