Can std::begin work with array parameters and if so, how?

后端 未结 5 2204
慢半拍i
慢半拍i 2020-12-03 02:08

I have trouble using std::begin() and std::end() (from the iterator library) with c-style array parameters.

void SetOr         


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 02:25

    While not directly answering your question (it has already been answered sufficiently by M M. and Dietmar Kühl), you appear to want to initialize some std::vector in this function. That said, why not just have:

    std::vector v;
    std::copy(std::begin(x), std::end(x), std::back_inserter(v));
    // or std::copy(x, x + 3, std::back_inserter(v));
    

    Instead of a function call to your function that is trying to do this?

    Alternatively, you could write you function like this:

    template
    void SetOrigin(RandomIterator start, RandomIterator end)
    {
        std::vector v;
        std::copy(start, end, std::back_inserter(v));
        SetOrigin(v);
    }
    

    and then call it using:

    double xyz[3];
    SetOrigin(std::begin(xyz), std::end(xyz));
    

提交回复
热议问题