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

前端 未结 9 1422
无人共我
无人共我 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:14

    • A stadard container works for homogenous objects 9which you can return).
    • The standard library way is to abstract an algorithm from the container and use iterators to bridge the gap between.
    • If you need to pass more than a single type think of structs/classes.

    My question is: Is the insert_iterator the right or standard way to do it?

    Yes. Otherwise, if you are not going to have at least as many elements in your container as there will be computed values. This is not always possible, specially, if you want to write to a stream. So, you are good.

提交回复
热议问题