我試圖通過將字符串提供給函數來減少字符串。例如:如果我有「abrakadabra」,函數將返回一串「abrkd」。這意味着所有字符只能在返回值中存在一次。我有以下功能:將char轉換爲C++中的字符串
string textletters(string text) {
string ret = "";
for (unsigned i = 0; i < text.length(); i++) {
if (i == 0) {
ret += str;
} else {
bool exist = false;
for (unsigned j = 0; j < i; j++) {
if (text.at(i) == text.at(j) {
exist = true;
}
}
if (!exist) {
ret += str;
}
}
}
return ret;
}
我知道 'text.at(I)' 給出了一個char回來。但是我想將這個字符轉換爲一個字符串,所以我可以連接它。
在此先感謝
你不必。 'std :: string'的運算符'+ ='可以附加一個'char'就好了。 –
[concatenate char \ * to string]可能重複(http://stackoverflow.com/questions/10553892/concatenate-char-to-string) – Boumbles
@Boumbles不是真的這個問題的一個騙局,但可能仍然是一個騙局。 – erip