以下示例将演示以下版本格式之间的比较:
major.minor.revision.build
或任何较短的版本,例如仅major
当它允许您扩展它以满足您的需求时,
"some of the version numbers may be very long like 2.3.2.2.3.1.1.5.3.5.6.2"
使用下面的示例,版本字符串的开头和结尾处的点被处理,直到.0.4
被认为等于0.0.4
并且.1.
被认为等于0.1.0
。
比较版本.h
#ifndef COMPAREVERSION_H_
#define COMPAREVERSION_H_
#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
struct CompareVersion {
public:
int maj;
int min;
int rev;
int build;
CompareVersion(std::string);
bool operator < (const CompareVersion&);
bool operator <= (const CompareVersion&);
bool operator == (const CompareVersion&);
friend std::ostream& operator << (std::ostream& stream, const CompareVersion& ver) {
stream << ver.maj;
stream << '.';
stream << ver.min;
stream << '.';
stream << ver.rev;
stream << '.';
stream << ver.build;
return stream;
};
void reset();
};
#endif /* COMPAREVERSION_H_ */
比较版本.cpp
#include "CompareVersion.h"
CompareVersion::CompareVersion(std::string version)
{
reset();
if (version.compare(0,1,".") == 0)
version = "0"+version;
if (version.compare(version.size()-1,1,".") == 0)
version.append("0");
sscanf(version.c_str(), "%d.%d.%d.%d", &maj, &min, &rev, &build);
if (maj <= 0) maj = 0;
if (min <= 0) min = 0;
if (rev <= 0) rev = 0;
if (build <= 0) build = 0;
}
bool CompareVersion::operator < (const CompareVersion& other)
{
if (maj < other.maj) return true;
if (min < other.min) return true;
if (rev < other.rev) return true;
if (build < other.build) return true;
return false;
}
bool CompareVersion::operator <= (const CompareVersion& other)
{
if (maj >= other.maj) return true;
if (min >= other.min) return true;
if (rev >= other.rev) return true;
if (build >= other.build) return true;
return false;
}
bool CompareVersion::operator == (const CompareVersion& other)
{
return maj == other.maj
&& min == other.min
&& rev == other.rev
&& build == other.build;
}
void CompareVersion::reset()
{
maj = 0;
min = 0;
rev = 0;
build = 0;
}
主文件
#include <iostream>
#include "CompareVersion.h"
using namespace std;
int main()
{
if((CompareVersion("1.2.3.4") == CompareVersion("1.2.3.4")) == true)
cout << "Version 1.2.3.4 and version 1.2.3.4 are equal" << endl;
if((CompareVersion("1.2.3.3") < CompareVersion("1.2.3.4")) == true)
cout << "Version 1.2.3.3 is smaller than 1.2.3.4. " << endl;
if((CompareVersion("1.2.3.4") < CompareVersion("1.2.3.4")) == true)
cout << "You won't see that. " << endl;
if((CompareVersion("1.2.3.4") <= CompareVersion("1.2.3.4")) == true)
cout << "Version 1.2.3.4 is smaller or equal to 1.2.3.4" << endl;
if((CompareVersion("1") <= CompareVersion("1.0.0.1")) == true)
cout << "Version 1 is smaller or equal to 1.0.0.1" << endl;
/* THE DOTS */
if((CompareVersion(".0.4") == CompareVersion("0.0.4")) == true)
cout << "Version .0.4 and version 0.0.4 are equal" << endl;
if((CompareVersion(".1.") == CompareVersion("0.1.0")) == true)
cout << "Version .1. and version 0.1.0 are equal" << endl;
if((CompareVersion("1") == CompareVersion("1.0.0.0")) == true)
cout << "Version 1 and version 1.0.0.0 are equal" << endl;
return 0;
}
输出
Version 1.2.3.4 and version 1.2.3.4 are equal
Version 1.2.3.3 is smaller than 1.2.3.4.
Version 1.2.3.4 is smaller or equal to 1.2.3.4
Version 1 is smaller or equal to 1.0.0.1
Version .0.4 and version 0.0.4 are equal
Version .1. and version 0.1.0 are equal
Version 1 and version 1.0.0.0 are equal