-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString_List_Serialize_Deserialize.cpp
More file actions
81 lines (68 loc) · 1.57 KB
/
String_List_Serialize_Deserialize.cpp
File metadata and controls
81 lines (68 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "stdafx.h"
#include <iostream>
#include <climits>
#include <vector>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
string serialize(vector<string> strs)
{
stringstream counter;
stringstream s;
for(int i=0;i<strs.size();i++)
{
counter<<strs[i].size()<<"_";
s<<strs[i];
}
counter<<"_";
counter<<s;
return counter.str();
}
vector<string> deserialize(string s)
{
vector<string> result;
size_t split=s.find("__");
if(split==string::npos)return result;
string counter=s.substr(0, split);
int sPos=split+2, countPos=0;
string::size_type st;
while(countPos<split)
{
int k=countPos;
while(k<split&&s[k]!='_')k++;
int itemLength=stoi(s.substr(countPos, k-countPos), &st);
result.push_back(s.substr(sPos, itemLength));
sPos+=itemLength;
countPos=k;
}
return result;
}
void testSerialize()
{
vector<string> strs;
string tmp=serialize(strs);
cout<<tmp<<endl;
for(int i=0;i<strs.size();i++)cout<<strs[i]<<endl;
cout<<"------------"<<endl;
strs.push_back("");
tmp=serialize(strs);
cout<<tmp<<endl;
for(int i=0;i<strs.size();i++)cout<<strs[i]<<endl;
cout<<"-----------"<<endl;
strs.push_back(" ");
tmp=serialize(strs);
cout<<tmp<<endl;
for(int i=0;i<strs.size();i++)cout<<strs[i]<<endl;
cout<<"-----------"<<endl;
strs.push_back(" abcd | \t\t\0");
tmp=serialize(strs);
cout<<tmp<<endl;
for(int i=0;i<strs.size();i++)cout<<strs[i]<<endl;
cout<<"-----------"<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
testSerialize();
return 0;
}