2011-12-05 19 views
4

我需要聲明一個字段調用「版本」,但不知道要使用哪種數據類型。什麼可能是C++中適用於軟件版本的正確數據類型?

我的「版本」就像「11.04」。我正在考慮將「雙倍」作爲一個好的候選人。

+11

我想象四捨五入發生並獲得免費:) – AraK

+1

下一個版本是這樣一個版本的營銷或工程版本?因爲一些營銷人員_will_想出了一個可愛的版本名稱而不是數字,因爲_math很難_ :( – MSalters

回答

4

double可能是一個壞主意,除非你打算使用Knuth風格的版本收斂在一個不合理的數字上。 int數組或字符串有什麼問題?

3

A double將是一個可怕的候選人,因爲浮點數不夠精確。

我會建議一個類與幾個int成員。只是超載一些操作員。

4

使用兩個整數 - 一個用於主要版本號,另一個用於次要版本號。或者,使用一個字符串:)雙重聽起來不像一個好的候選人,因爲很多數字不能完全由雙打來表示。

2

我想我會使用array<unsigned char, 4>左右。這允許諸如11.2.1.3之類的東西,如果你最終需要它,並且仍然使用比雙倍更少的空間。 我還沒有看到版本號超過255的單個組件,所以 版本號超過255的單個組件是非常罕見的,我懷疑爲每個組件使用char是否真的是一個限制。如果您決定將每日版本號嵌入到版本號中,則會有明顯的例外。

如果你真的想要的多功能性,你可以這樣做:

typedef unsigned char component_t; 

array<component_t, 4> version; 

只是有一個陷阱與此:讀它,你需要知道component_t是什麼。處理這個問題的明顯方法是使用一個單字節的版本號來告訴您使用的版本號是什麼版本,所以無論何時您更改component_t(或允許的組件數),您都可以增加它! :-)

+0

@ildjarn:好的,我已經稍微改寫了一下。 –

2

使用int並讓1000對應版本1.0,很簡單。

1

一些圖書館使用像招數:

#define PKG_MAJOR (3) // example values... 
#define PKG_MINOR (7) 
#define PKG_MICRO (11) 

const unsigned long pkg_version = (PKG_MAJOR * 1000 + PKG_MINOR) * 1000 + PKG_MICRO; 

即pkg_version是3007011.

1

有關使用unsigned int還是這樣嗎?我剛纔看到這個在linux kernel Makefile

#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) 

你有一個內核版本a,主要版本b和次要版本c。您可以輕鬆比較兩個版本的順序。如果你想讓這個版本具有很好的可打印格式,你可以添加一個函數來創建一個版本字符串,例如"2.6.38"。 (當然,您被限制爲最多256個子版本...使用unsigned long long。?)

2

我用semantic versioning,因此我創建了以下用戶定義類型:

版本。HPP

#include <string> 
#include <cstdlib> 
#include <iostream> 

class Version_Number{ 

public: 

    //constructors 
    Version_Number(); //<- constructs to 0.0.0 
    Version_Number(std::string const& version_number); 

    //transformers 
    void Major_Update(); 
    void Minor_Update(); 
    void Bug_Update(); 

    //observers 
    std::string str(); 
    friend std::ostream& operator<<(std::ostream& os, const Version_Number& r); 
    bool operator < (Version_Number const& other) const; 
    bool operator > (Version_Number const& other) const; 
    bool operator == (Version_Number const& other) const; 
    bool operator != (Version_Number const& other) const; 

private: 
    unsigned int a,b,c; 

}; 

version.cpp

#include <string> 
#include <cstdlib> 
#include <iostream> 

#include "version_number.hpp" 

    Version_Number::Version_Number(): a(0),b(0),c(0){} 

    Version_Number::Version_Number(std::string const& folder_name){ 

     std::string a_str,b_str,c_str; 

     auto it = folder_name.begin(); 
      while (*it != '.'){ 
       a_str+=*it; 
       ++it; 
      } 
      ++it; 
      while (*it != '.'){ 
       b_str+=*it; 
       ++it; 
      } 
      ++it; 
      while (it != folder_name.end()){ 
       c_str+=*it; 
       ++it; 
      } 

      a = std::atoi(a_str.c_str()); 
      b = std::atoi(b_str.c_str()); 
      c = std::atoi(c_str.c_str()); 
    } 

    void Version_Number::Major_Update(){ 
     ++a; 
     b = 0; 
     c = 0; 
     return; 
    } 
    void Version_Number::Minor_Update(){ 
     ++b; 
     c = 0; 
     return; 
    } 
    void Version_Number::Bug_Update(){ 
     ++c; 
     return; 
    } 

    std::string Version_Number::str(){ 
     std::string str; 

     str+= std::to_string(a); 
     str+='.'; 
     str+= std::to_string(b); 
     str+='.'; 
     str+= std::to_string(c); 

     return str; 
    } 
bool Version_Number::operator < (Version_Number const& other) const{ 
    if (a > other.a){return false;} 
    if (a < other.a){return true;} 
    if (b > other.b){return false;} 
    if (b < other.b){return true;} 
    if (c > other.c){return false;} 
    if (c < other.c){return true;} 
    return false; 
} 

bool Version_Number::operator > (Version_Number const& other) const{ 
    if (a < other.a){return false;} 
    if (a > other.a){return true;} 
    if (b < other.b){return false;} 
    if (b > other.b){return true;} 
    if (c < other.c){return false;} 
    if (c > other.c){return true;} 
    return false; 
} 

bool Version_Number::operator == (Version_Number const& other) const{ 
    if (a == other.a){ 
     if (b == other.b){ 
      if (c == other.c){ 
       return true; 
      } 
     } 
    } 

    return false; 
} 
bool Version_Number::operator != (Version_Number const& other) const{ 
    if (a == other.a){ 
     if (b == other.b){ 
      if (c == other.c){ 
       return false; 
      } 
     } 
    } 

    return true; 
} 

std::ostream& operator<<(std::ostream& os, const Version_Number& r){ 
    os << r.a << '.' << r.b << '.' << r.c; 
    return os; 
} 
相關問題