c++ - Efficient parsing of std::stringstream data into std::vector<std::vector> -


I get std :: vector & gt; I am parsing the data stream of pairs of double values ​​in. I encourage because I think it is more efficient. My code is as follows.

  std :: stringstream tmp_stream; Typedef Double Data_type; Typedef :: std :: vector & lt; Data_type & gt; V_d; // Type below the type of container of all data typedef :: std :: vector & lt; V_d> V_v_d; // Type the container // Type: std :: list & lt; V_d> V_v_d; V_v_d data; :: data_parse :: data_pacer & lt; V_v_d> Data_parser; Data_parser (tmp_stream, data);   

My input text file is in {(132.181,0.683431), (136.886,0.988517), (137.316,0.504297), (133.653,0.60229), (150.86,0.236839) Have been formatted in) pairs are not correctly parsed and I got a blank pair What could be the problem? Thanks

By using the Boost's soul, you can use one line:

  if (tmp_stream> gt; std :: noskipws >> gt; qi :: phrase_match ((+ qi :: double _)% qi :: eol, qi :: blank, data) ) {  

And one more line to display results in the form of a bonus:

  std :: cout & lt; & Lt; Action :: Format ((karma :: double_% ","% Karma :: EOL, data)   

Note < / Strong> This handles inf , -inf , Nine ):

See this :

  #include & lt; Boost / spirit / include / qi.hpp & gt; #include & lt; Boost / spirit / include / karma.hpp & gt; #include & lt; Boost / spirit / include / qi_match.hpp & gt; Namespace qi = boost :: spirit :: qi; Nameshot Karma = Promotion: Spirit :: Karma; Int main () {std :: vector & lt; Std :: vector & lt; Double & gt; & Gt; Information; Std :: stringstream tmp_stream ("123 45 inf -inf nan .7e-99 \ n" "42 \ n" "-1"); If (tmp_stream>> std :: noskipws>> qi :: phrase_match ((+ qi :: double _)% qi :: eol, qi :: blank, data)) {std :: cout & Lt; & Lt; Action :: Format ((karma :: double_% ","% Karma :: EOL, data)   

UPDATE Your input grammar has been slightly modified, I instead suggest parsing in a pair of pairs:

  #include  gt; std :: noskipws & gt; qi: phrase_match ('{'>>> ('('> 'gti: double_ & gt; ;> "," & Gt; & gt; Qi :: double_ & gt; & gt; ')'% ',' & gt; & Gt; '}', Qi :: location, data)) {std :: cout & lt; & Lt; Karma :: format (karma :: border ("") [karma :: auto_]% karma :: eol, data) & lt; & Lt; "\ N"; }}   

Which prints: (

  132.181 0.683 136.886 0.989 137.316 0.504 133.653 0.602 150.86 0.237  
  • streambuf_iterator ,
  • Built-in byte data if you have it in memory

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -