したがって、TCP winsock 接続を介して受信されている次のデータ文字列があり、各構造体が 1 つのレコードを表す構造体のベクトルに高度なトークン化を行いたいと考えています。
std::string buf = "44:william:adama:commander:stuff\n33:luara:roslin:president:data\n"
struct table_t
{
std::string key;
std::string first;
std::string last;
std::string rank;
std::additional;
};
文字列内の各レコードは、キャリッジ リターンで区切られます。レコードを分割しようとしましたが、まだフィールドを分割していません:
void tokenize(std::string& str, std::vector< string >records)
{
// Skip delimiters at beginning.
std::string::size_type lastPos = str.find_first_not_of("\n", 0);
// Find first "non-delimiter".
std::string::size_type pos = str.find_first_of("\n", lastPos);
while (std::string::npos != pos || std::string::npos != lastPos)
{
// Found a token, add it to the vector.
records.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of("\n", pos);
// Find next "non-delimiter"
pos = str.find_first_of("\n", lastPos);
}
}
コロン (内部フィールドセパレーター) を介して各レコードをさらにトークン化し、各構造体をベクターにプッシュするために、そのすべてのコードをもう一度繰り返す必要はまったくないようです。これを行うためのより良い方法があると確信しているか、設計自体が間違っている可能性があります。
助けてくれてありがとう。