Best way to return list of objects in C++?

前端 未结 6 2096
眼角桃花
眼角桃花 2021-02-12 08:56

It\'s been a while since I programmed in C++, and after coming from python, I feel soooo in a straight jacket, ok I\'m not gonna rant.

I have a couple of functions that

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-12 09:33

    The only thing I can see is that your forcing a copy of the list you return. It would be more efficient to do something like:

      void DoSomething(const std::vector& in, std::vector& out)
      {
      ...
      // no need to return anything, just modify out
      }
    

    Because you pass in the list you want to return, you avoid the extra copy.

    Edit: This is an old reply. If you can use a modern C++ compiler with move semantics, you don't need to worry about this. Of course, this answer still applies if the object you are returning DOES NOT have move semantics.

提交回复
热议问题