このようなものはチェックを行うために機能し、かなり最小限です。ブーストを使用して文字列を分割し、バージョンを段階的に比較します。欠落している先行ゼロを自動的に処理します。
#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
#include <iostream>
int version_a_newer_than_b(const std::string& a, const std::string& b)
{
// First, split the string.
std::vector<std::string> va, vb;
boost::split(va, a, boost::is_any_of("."));
boost::split(vb, b, boost::is_any_of("."));
// Compare the numbers step by step, but only as deep as the version
// with the least elements allows.
const int depth = std::min(va.size(), vb.size());
int ia,ib;
for (int i=0; i<depth; ++i)
{
ia = atoi(va[i].c_str());
ib = atoi(vb[i].c_str());
if (ia != ib)
break;
}
// Return the required number.
if (ia > ib)
return 1;
else if (ia < ib)
return -1;
else
{
// In case of equal versions, assumes that the version
// with the most elements is the highest version.
if (va.size() > vb.size())
return 1;
else if (va.size() < vb.size())
return -1;
}
// Everything is equal, return 0.
return 0;
}
int main()
{
std::string a = "0.1.32.8";
std::string b = "0.1";
std::cout << "Is a newer than b: " << version_a_newer_than_b(a, b) << std::endl;
return 0;
}