2013-08-01 44 views
0

我正在使用C++使用分隔符標記字符串,並且可以在while循環中使用cout輸出當前標記。我想要做的是將令牌的當前值存儲在一個數組中,以便稍後可以訪問它。下面是代碼我現在有:將字符串標記存儲到數組中

string s = "Test>=Test>=Test"; 
string delimiter = ">="; 
vector<string> Log; 
int Count = 0; 
size_t pos = 0; 
string token; 
while ((pos = s.find(delimiter)) != string::npos) { 
token = s.substr(0, pos); 
strcpy(Log[Count].c_str(), token.c_str()); 
Count++; 

s.erase(0, pos + delimiter.length()); 
} 
+0

當然,這不會編譯。如何將'strcpy()'放入'c_str()'調用的結果中?您應該簡單地將該標記push_back()到Log中。 –

回答

1

的載體只需使用push_back。它會爲你的矢量做一個副本。沒有必要保持計數;無需strcpy

string s = "Test>=Test>=Test"; 
string delimiter = ">="; 
vector<string> Log; 
size_t pos = 0; 
string token; 
while ((pos = s.find(delimiter)) != string::npos) { 
    token = s.substr(0, pos); 
    Log.push_back(token); 
    s.erase(0, pos + delimiter.length()); 
} 
相關問題