C++ string parsing (python style)

后端 未结 10 1179
伪装坚强ぢ
伪装坚强ぢ 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

    All these good examples aside, in C++ you would normally override the operator >> for your point type to achieve something like this:

    point p;
    while (file >> p)
        points.push_back(p);
    

    or even:

    copy(
        istream_iterator(file),
        istream_iterator(),
        back_inserter(points)
    );
    

    The relevant implementation of the operator could look very much like the code by j_random_hacker.

提交回复
热议问题