vector serialization

后端 未结 4 832
挽巷
挽巷 2020-12-19 06:44

I am trying to binary serialize the data of vector. In this sample below I serialize to a string, and then deserialize back to a vector, but do not get the same data I start

4条回答
  •  余生分开走
    2020-12-19 07:05

    To serialize this vector into a string, You first want to convert each of the elements of of this vector from an int into a string containing the same the ascii representation of that number, this operation can be called serialization of an int to string.

    So for example, assuming an integer is 10 digits we can

    // create temporary string to hold each element
    char intAsString[10 + 1];
    

    then convert the integer to a string

    sprintf(intAsString, "%d", v[0]);
    

    or

    itoa( v[0], intAsString, 10 /*decimal number*/ );
    

    You can also make use of the ostringstream and the << operator

    if you look at the memory contents of intAsString and v[0], they are very different, the first contains the ascii letters that represent the value of v[0] in the decimal number system(base 10) while v[0] contains the binary representation of the number(because that's how computers store numbers).

提交回复
热议问题