How do I return hundreds of values from a C++ function?

前端 未结 9 1467
无人共我
无人共我 2021-01-01 04:37

In C++, whenever a function creates many (hundreds or thousands of) values, I used to have the caller pass an array that my function then fills with the output values:

9条回答
  •  爱一瞬间的悲伤
    2021-01-01 05:32

    I'd use something like

    std::auto_ptr > computeValues(int input);
    {
       std::auto_ptr > r(new std::vector);
       r->push_back(...) // Hundreds of these
       return r;
    }
    

    No copying overhead in the return or risk of leaking (if you use auto_ptr correctly in the caller).

提交回复
热议问题