id<255>name<000>1<255>科技<000>7<255>河南
這是一串字符串 要轉成如下格式的二維數組
[[id,name],[1,科技],[7,河南]]
求代碼學習
完全滿足你的要求!
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> split(string strTime)
{
vector<string> result;
string temp("");
bool part = false;
for(size_t i = 0; i < strTime.size(); )
{
if(strTime[i] == '<')
{
result.push_back(temp);
temp = "";
while(strTime[i]!='>')
i++;
i++;
continue;
}
else
{
temp += strTime[i];
i++;
}
}
result.push_back(temp);
return result;
}
int main()
{
string test("id<255>name<000>1<255>科技<000>7<255>河南");
vector<string> result = split(test);
for(size_t i = 0; i < result.size(); i++)
{
cout<<result[i]<<" ";
}
cout<<endl;
string fina;
string head("[[");
string tail("]]");
string mid("],[");
//[[id,name],[1,科技],[7,河南]]
for(size_t i = 0; i < result.size(); i++)
{
if((i+1)%2==0 && i!=0 &i!=result.size()-1)
{
fina +=result[i];
fina+=mid;
}
else
{
fina += result[i];
if(i!= result.size()-1)
fina +=",";
}
}
fina = head + fina + tail;
cout<<fina<<endl;
return 0;
}