Not a quite secret, just have some operator overload trick.
|
|
This method have several benefit, first, std::vector
can be inserted in templated class, to replace any stream :
|
|
It can load up data into container, with controlled type. If you wish, you can even load any byte data into it(using first two implementations). Unloading a vector is quite easy, just use
|
|
It’s easy, but there is 2 important highlight and ignoring these will easily mess up your code:
- This data have the same life cycle with the vector container, if the vector is destoryed, this data will no longer valid.
- This data should use
const
to get the data! missingconst
will directly changes data in vector, and cause unexpected side effect, be advised!
For sake of safely, you should get this data via memcpy, or you know what you are doing. in general case, put both dumpData
and vectorContainer
in the same scope is a good idea, and it is common sense that you should not pass dumpData with pointer to outside of the scope.
近期评论