#include<iostream>
#include<vector>
#include<string>
using namespace std;
struct AttrAddon
{
int index;
int addon;
};
typedef vector<AttrAddon> AttrAddonVector;
typedef vector<string> StringVector;
namespace StringUtility
{
StringVector split(const std::string& str, const std::string& delims )
{
int maxSplits = 0;
StringVector ret;
int numSplits = 0;
size_t start = 0;
size_t pos = 0;
do
{
pos = str.find_first_of(delims, start);
if (pos == start)
{
start = pos + 1;
}
else if (pos == std::string::npos || (maxSplits && numSplits == maxSplits))
{
ret.push_back( str.substr(start) );
break;
}
else
{
ret.push_back( str.substr(start, pos - start) );
start = pos + 1;
}
start = str.find_first_not_of(delims, start);
++numSplits;
} while (pos != std::string::npos);
return ret;
}
}
int main()
{
string str = "1:400|2:248"; //人物初始属性,如1血条:400 2攻击:248
AttrAddonVector attrAddons;
AttrAddon attrAddon = {};
if(!str.empty() && str != "0")
{
StringVector strAttrAddons = StringUtility::split(str, "|");
for (StringVector::iterator it = strAttrAddons.begin(); it != strAttrAddons.end(); ++it)
{
StringVector strAttrAddon = StringUtility::split(*it, ":");
if (strAttrAddon.size() == 2)
{
attrAddon.index = atoi(strAttrAddon[0].c_str());
attrAddon.addon = atoi(strAttrAddon[1].c_str());
if (attrAddon.index > 0 && attrAddon.addon > 0)
{
attrAddons.push_back(attrAddon);
}
}
else
{
cout<<"Logerr"<<endl;
}
}
}
for(int i=0; i<attrAddons.size(); i++)
{
cout<<attrAddons[i].index<<endl;
cout<<attrAddons[i].addon<<endl;
}
vector<AttrAddon>::iterator attr = attrAddons.begin();
while(attr != attrAddons.end())
{
cout<<(*attr).index<<endl;
cout<<(*attr).addon<<endl;
attr++;
}
return 0;
}