-1
vector<string> Util::split(string str, char delimiter){
vector<string> internal;
stringstream ss(str); // Turn the string into a stream.
string tok;
while(getline(ss, tok, delimiter)) {
internal.push_back(tok);
}
return internal;
}
string str =「k 1 1」;將字符串拆分爲C++中的單獨變量
函數getline(β,TOK,'「)
如果我嘗試檢查大小爲返回向量是大小ONE。
string str =「k,1,1」;
函數getline(β,TOK, '')
然而,如果我改變定界符爲逗號和修改字符串變量TOK。
規模將然後是三個
因此,有白色空間
問題我能做些什麼,使這項工作?我幾乎整整一天都在這上面。
int main(){
Util src;
int runProgram = 1;
string input;
while(runProgram!=0){
cout << "Input your command to perfrom operation: " << endl;
cin >> input; // user inputs "k 1 1"
vector<string>sep=src.split(input, ' ');
cout << sep.size() << endl; // size is one when it should be 3
}
}
所顯示的代碼將不能不管工作怎樣被投入'tok',因爲'stringstream'會從名爲'str'一些其他的,神祕的變量構成。根據描述,它看起來像我所示的代碼不是真實代碼,而是幻想代碼。一個真正的問題的真實答案將需要真正的代碼,而不是幻想代碼。 –
你是對的我的錯誤 – Marke
你的代碼如現在所示[完美地工作](http://coliru.stacked-crooked.com/a/062c49efb0278e67)。請提供實際存在問題的[mcve]。 – zett42