C++ 中 字符串转二维数组的问题

 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;
}

输出如下:
图片说明

这种写法略微简单粗暴。而且代码逻辑晦涩难懂。