我正在使用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 :(得分: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());
}
答案 1 :(得分:0)
push_back()
会做你需要的。