0
如何拆分std :: string以便結構對象的值被填充。如何從boost :: split獲取多個參數
q.Qcmd = "Command1"
q.timeValue = 1.0
這是示例代碼。
struct QueueCommand
{
std::vector<std::string>QCmd;
std::vector<float>timeValue;
};
int _tmain(int argc, _TCHAR* argv[])
{
std::string str = "command1|1.0"
std::string str1 = "command2|2.0"
QueueCommand q;
boost::split(q,str,boost::is_any_of("|")); // need to fill Qcmd and timevalue
boost::split(q,str1,boost::is_any_of("|"));
return 0;
}
我會先*做'q.timeValue = boost :: lexical_cast(parts.back());',然後'parts.pop_back();'然後'q.QCmd = std :: move(部分)'。看起來既簡單又高效。如果你想壓縮最後一個字節,在'pop_back()'之後做'shrink_to_fit()'。 (我假設你的'reserve()'是爲了'resize()',否則該向量的大小可能是錯誤的/行爲未定義。) –
5gon12eder
啊,你是對的。非常好的建議。我正在更改'reserve',但如果您想添加其他答案,我會留下您的代碼。 –
我很高興你喜歡它;如果您覺得合適,請隨時採用。我不打算自己發佈答案。 – 5gon12eder