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());
}
4

2 回答 2

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());
}
于 2013-08-01T15:16:20.153 回答
0

push_back()会做你需要的。

于 2013-08-01T16:20:08.777 回答