C++ string parsing (python style)

后端 未结 10 1155
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 07:52

I love how in python I can do something like:

points = []
for line in open(\"data.txt\"):
    a,b,c = map(float, line.split(\',\'))
    points += [(a,b,c)]
<         


        
10条回答
  •  一生所求
    2020-12-08 08:37

    Its nowhere near as terse, and of course I didn't compile this.

    float atof_s( std::string & s ) { return atoi( s.c_str() ); }
    { 
    ifstream f("data.txt")
    string str;
    vector> data;
    while( getline( f, str ) ) {
      vector v;
      boost::algorithm::split_iterator e;
      std::transform( 
         boost::algorithm::make_split_iterator( str, token_finder( is_any_of( "," ) ) ),
         e, v.begin(), atof_s );
      v.resize(3); // only grab the first 3
      data.push_back(v);
    }
    

提交回复
热议问题