// 版本号拆分为数组

void splitToInt(string str , vector<int> *v1, char delim )
{
// 拆分
string strTmp;
strTmp = "";
int num = 0;
for (int i = 0;i< str.size();i++)
{
if (str[i] == '.')
{
num = atoi(strTmp.c_str());
v1->push_back(num);
strTmp = "";
}else
{
strTmp += str[i];
}
}
num = atoi(strTmp.c_str());
v1->push_back(num);
}

// 比较字符串版本

int compareVersion(string str1, string str2)
{
vector <int> v1;
vector <int> v2;

splitToInt(str1,&v1);
splitToInt(str2,&v2);

// 比较
int sizeV1 = v1.size();
int sizeV2 = v2.size();
//if(sizeV1>sizeV2) return 1;
//if(sizeV1<sizeV2) return -1;
int sizeV = sizeV1>sizeV2?sizeV2:sizeV1;
for( int i=0; i<sizeV; i++)
{
if(v1[i]>v2[i]) return 1;
if(v1[i]<v2[i]) return -1;
}
return 0;

}

posted on 2018-07-17 16:22  swing07  阅读(874)  评论(0编辑  收藏  举报