C++ stl stringstream direct buffer access

后端 未结 4 1511
囚心锁ツ
囚心锁ツ 2020-12-29 20:31

this should be pretty common yet I find it fascinating that I couldn\'t find any straight forward solution.

Basically I read in a file over the network into a string

4条回答
  •  我在风中等你
    2020-12-29 20:44

    std::stringstream doesn't (necessarily) store its buffer contiguously but can allocate chunks as it is gradually filled. If you then want all of its data in a contiguous region of memory then you will need to copy it and that is what str() does for you.

    Of course, if you want to use or write a class with a different storage strategy then you can, but you don't then need to use std::stringstream at all.

提交回复
热议问题