C++ string parsing (python style)

后端 未结 10 1163
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  萌比男神i
    2020-12-08 08:34

    The C++ String Toolkit Library (StrTk) has the following solution to your problem:

    #include 
    #include 
    #include "strtk.hpp"
    
    struct point { double x,y,z; }
    
    int main()
    {
       std::deque points;
       point p;
       strtk::for_each_line("data.txt",
                            [&points,&p](const std::string& str)
                            {
                               strtk::parse(str,",",p.x,p.y,p.z);
                               points.push_back(p);
                            });
       return 0;
    }
    

    More examples can be found Here

提交回复
热议问题