如何比较C ++中的版本号

时间:2015-10-14 20:30:30

标签: c++ string-comparison version-numbering

我们的教授希望我们编写一个程序来比较两个版本号,例如0.1&lt; 0.2或1 < 1.1。还有一些技巧,如.0.4&lt; 0.1。 所以,我的想法首先判断数字是否以点开头,如果是,我向它添加0。之后我删除了除第一个之外的其他点。然后我将字符串转换为数字并进行比较。这是我在第一步中所做的。

string numb1,numb2;
if(numb1[0]=='.')
{
    numb1 ="0"+ numb1;
}

我对第二个数字做同样的事情。现在我需要帮助向我展示如何除去第一个点之外的点。 我们的教授希望我们使用这个特定的功能: int compareVersions(string ver1,string ver2)。 如果ver1> ver2:返回1 如果ver1&lt; ver2:返回-1 否则返回0.

顺便说一下,有些视觉数字可能很长,如2.3.2.2.3.1.1.5.3.5.6.2或1.1.1.1.1.1.1。

5 个答案:

答案 0 :(得分:5)

这是一种适用于数字版本号的方法:

  • 使用getline(strstream, token, ".")
  • 将输入字符串拆分为多个部分
  • 使用atoistol将相应的片段转换为数字,并以数字方式进行比较

基本上,将版本号视为由.分隔的数字序列,并按字典顺序比较这些序列。

请注意,实际的通用版本号比较算法可能需要处理额外的技巧,例如字母后缀(例如1.1e2.4b243.5rc1)等。我&#39; m假设这超出了类练习的范围,但方法类似:将这些部分拆分为数字和非数字部分的序列并比较每个部分(例如2.4b7 < 2.4b24因为4, "b", 7 < 4, "b", 24)。

答案 1 :(得分:1)

因为您知道numb[1]等于&#39;。&#39;你可以使用

 numb1.erase(std::remove(numb1.begin() + 2, numb1.end(), '.'), numb1.end());

在第二个字符后删除numb1中的所有点。

答案 2 :(得分:0)

你需要做的是遍历字符串,忽略'。'并将数字的char表示转换为int。然后比较两个最终结果。

string numb1 = "4.3.2";
string numb2 = "3.4.5";
int n1 = 0;
int n2 = 0; 

for (int i = 0; i < numb1.length(); i++)
{
    if (numb1[i] != '.')
    {   
        n1 = n1 * 10;
        n2 = n2 * 10;
        n1 += (int(numb1[i]) - '0');
        n2 += (int(numb2[i]) - '0');                
    }

}

那将给你432和345,比较那些会给你哪个更高的版本。

答案 3 :(得分:0)

像这样的东西可以用来进行检查并且相当小。它使用boost来拆分字符串,然后逐步比较版本。它会自动处理缺少的前导零。

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

答案 4 :(得分:-1)

以下示例将演示以下版本格式之间的比较: 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

<强> CompareVersion.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_ */

<强> CompareVersion.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;
}

<强>的main.cpp

#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