Is there a more efficient way to set a std::vector from a stream?

前端 未结 4 1769
别那么骄傲
别那么骄傲 2021-01-18 03:37

Presently, I set the value of a std::vector from an std::ostringstream as follows:

void
foo(std::vector &am         


        
4条回答
  •  囚心锁ツ
    2021-01-18 04:10

    if there is a more efficient way

    You may want to call reserve on your data and use the range insert member directly on data instead of using a copy-assignment. The thing about vectors that you need to remember is that every node may potentially increase the size (and relocate all elements). So, you're better off allocating the memory in one go (if you know how many objects you will be storing -- which you do know here) and leverage this fact.

提交回复
热议问题