| Home | Libraries | People | FAQ | More

#### Number List - stuffing numbers into a std::vector

This sample demonstrates a parser for a comma separated list of numbers. The numbers are inserted in a vector using phoenix.

    template <typename Iterator>
    bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v)
    {
        using qi::double_;
        using qi::phrase_parse;
        using qi::_1;
        using ascii::space;
        using phoenix::push_back;
        using phoenix::ref;

        bool r = phrase_parse(first, last,

            //  Begin grammar
            (
                double_[push_back(ref(v), _1)]
                    >> *(',' >> double_[push_back(ref(v), _1)])
            )
            ,
            //  End grammar

            space);

        if (first != last) // fail if we did not get a full match
            return false;
        return r;
    }

The full cpp file for this example can be found here: ../../example/qi/num_list2.cpp

This, again, is the same parser as before. This time, instead of summing up the numbers, we stuff them in a `std::vector`. `push_back` is supplied by Phoenix. The expression:

    push_back(ref(v), _1)

appends the parsed number. Like before, `_1` is a Phoenix placeholder for the parsed result attribute. Also, like before, `ref(v)` tells Phoenix that `v`, the `std::vector`, is a mutable reference.