C++入門簡單一實例

來源:互聯網
上載者:User

教授C++,如果還使用Hello World的例子作為入門,那就弱爆啦!它只能告訴你不要用printf,要用cout。但是,為什嗎?這兩個有什麼區別?cout就代表了C++嗎?我們還是整點有技術含量的吧。

問題編寫“字串與數值互相轉換”的函數。

1. C語言風格
/* C 風格編碼*/#include <cstdlib>#include <cstdio>using namespace std;void value2str(int value){    char str[30] = {'\0'};    itoa(value, str, 10); //C標準庫有這個函數嗎?    printf("string : %s\n", str);}void str2value(char* str){    int value = atoi(str);    printf("value : %d\n", value);    }

這種方式至少存在如下幾個問題:(1)字串的長度有限制;(2)必須預先定義一個數組儲存字串,使用數組也有記憶體泄露的危險;(3)調用C的庫函數,受制於函數參數約束。

2. C++風格
/* C++ 風格編碼*/#include <string>#include <sstream>using namespace std;/* 整型-> 字串*/string ValueToStr(int value){    ostringstream ost;    ost << value;    return ost.str();}/* 字串-> 整型*/int StrToValue(char* str){    string strValue(str); //轉換為string    istringstream osValue;    osValue.str(strValue); //得到istrstream    int value;    osValue >> value; //得到數值    return value;}

使用C++的string stream,可處理變長字串,無記憶體泄露之虞。但無擴充性,因為如果要求是double類型與字串互相轉換,需要再寫兩個重載函數。

3. 函數模板

/* 函數模板*/template <typename T>string Value2Str(T value){    ostringstream ost;    ost << value;    return ost.str();}template<typename T> T Str2Value(char* str){    string strValue = str; //轉換為string    istringstream osValue;    osValue.str(strValue); //得到istrstream    T value;    osValue >> value; //得到數值    return value;}

使用template,可處理各種資料類型和字串的轉換,使用者代碼中調用了哪個類型,編譯器才會編譯相應的目標代碼,靈活性很大。

當然這個例子中還要考慮字串轉換出來的數值,不能超出資料類型的最大值,這個不是講述的重點。重點在於,通過這幾種風格的代碼,展現一下為瞭解決一個簡單的問題,在C++中我們到底能有多少選擇。

我是因為平時編寫代碼時需要用到上述功能,用的多了,就慢慢總結出了這麼一套做法,當然比較粗糙,下面來看個不粗糙的。

某日翻閱boost的文檔,發現boost裡居然已經提供了類似功能的lexical_cast。

其定義形式如下:

namespace boost{    class bad_lexical_cast;    template<typename Target, typename Source>    Target lexical_cast(const Source& arg);}

也是採用模板的形式定義的一套東西,用於string和數值的相互轉換。簡單的使用例子如下:

#include <boost/lexical_cast.hpp>//下面的例子把一系列數字作為命令列參數:int main(int argc, char * argv[]){        using boost::lexical_cast;       using boost::bad_lexical_cast;    std::vector<short> args;    while(*++argv)        {        try        {            args.push_back(lexical_cast<short>(*argv));            }        catch(bad_lexical_cast &)              {            args.push_back(0);            }    }    //...}//下面的例子使用字串運算式來表示數字:void log_message(const std::string &);void log_errno(int yoko){    log_message("Error " + boost::lexical_cast<std::string>(yoko) + ": " + strerror(yoko));}

和我的設計具有相同的一個缺陷,浮點數的精度無法控制。更詳細的內容,請參見boost庫的相關文檔。

結語:從上面的例子我們可以看出,同樣是使用C++語言,不同的編程風格寫出來的代碼卻相差很大。C++功力越深,寫出來的代碼越抽象,越難以理解,但越靈活,越簡潔。當然,代碼寫得越簡潔,也有老闆越覺得你工作偷懶的危險^_^。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.