c++ - How to get multiple parameters from boost::split -


how can split std::string both values of structure object filled.

q.qcmd = "command1" q.timevalue = 1.0 

this sample code.

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 fill qcmd , timevalue  boost::split( q,str1,boost::is_any_of("|"));  return 0; } 

this not correct usage of boost::split because first parameter has container string, , split not know how fill specific structure. give hints on how solve it. have not tested code, can try yourself:

first, have declare vector store parts:

std::vector<std::string> parts; 

then, boost::split can split command string:

boost::split( parts, str, boost::is_any_of("|")); 

reserve enough space in corresponding queuecommand variable:

q.qcmd.resize(parts.size() - 1); 

(the last 1 contains float number). copy strings structure. have make sure parts array contains @ least 2 elements:

std::copy(parts.begin(), parts.begin() + parts.size() - 1, q.qcmd.begin()); 

set float part of struct:

q.timevalue = boost::lexical_cast<float>(parts[parts.size() - 1]); 

5gon12eder suggestion in comments:

which seems more elegant , efficient, requires c++11 std::move:

q.timevalue = boost::lexical_cast<float>(parts.back()); parts.pop_back(); q.qcmd = std::move(parts); 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -