2016-04-18 86 views
1

這裏是我運動代碼.. 書的問題是重構這個代碼模板(與STL)如何用STL重構這段代碼?

我發現了一些書和谷歌,但我不明白這一點

你能告訴我請舉例? ?

int SumInt(const int* a, int count) { 
    int result = 0; 
    for (int i = 0; i < count; ++i) { 
    result += a[i]; 
    } 
    return result; 
} 

float SumFloat(const float* a, int count) { 
    float result = 0; 
    for (int i = 0; i < count; ++i) { 
    result += a[i]; 
    } 
    return result; 
} 
void main() { 
    int intVals[] = {0, 1, 2}; 
    float floatVals[] = {0.F, 1.F, 2.F}; 

    int intTotal = SumInt(intVals, 3); 
    float floatTotal = SumFloat(floatVals, 3); 

.... 
} 
+0

從'std :: vector'開始取出原始數組:'std :: vector intVals {0,1,2};''std :: vector floatVals = {0.F,1.F ,2.F};' –

+1

http://en.cppreference.com/w/cpp/algorithm/accumulate –

+2

如果你的書告訴你'void main',它不是一本C++書。 –

回答

-1

您使用這樣的模板。還可以閱讀他們:http://www.cplusplus.com/doc/oldtutorial/templates/

template<typename T> 
T doSomething(const T a, int count) 
{ 
    // do your stuff 

} 

你使用這樣的:

int w = 1; 
doSomething(w, 1); 

您與int在這種情況下更換T

+0

在這種情況下,您不需要指定''。編譯器可以推斷出它。只需調用'doSomething(1,1)' – DeathTails

+0

是剛剛修改thath @DeathTails謝謝 – Ceros

5

您可以使用std::accumulate

int intTotal = std::accumulate(std::begin(intVals), std::end(intVals), 0); 
float floatTotal = std::accumulate(std::begin(floatVals), std::end(floatVals), 0.0F); 
2

在這個例子中,你可以用一個模板Sum同時取代SumIntSumFloat如下:

template <typename T> int Sum(const T* a, int count) { 
    T result = 0; 
    for (int i = 0; i < count; ++i) { 
     result += a[i]; 
    } 
    return result; 
} 

然後在你的main(),你可以叫

int intTotal = Sum(intVals, 3); 
float floatTotal = Sum(floatVals, 3); 

和編譯器會調用要麼intfloat版本取決於您提供給Sum的參數類型。

+0

基本上這個想法是用一個通用的替換一個特定的數據類型(int這種情況下'int'或'float')。人們經常使用'T'作爲泛型的符號。 – Logicrat

1

這裏,我們去(你和一個簡單的模板(...)函數):

template <typename T> 
T sum(const T* a, size_t count) { 
    T result = 0; 
    for (size_t i = 0; i < count; ++i) { 
     result += a[i]; 
    } 
    return result; 
} 

int main() { 
    const int intVals[] = { 0, 1, 2 }; 
    const float floatVals[] = { 0.F, 1.F, 2.F }; 

    const int intTotal = sum(intVals, 3); 
    const float floatTotal = sum(floatVals, 3); 

    std::cout << "float total: " << intTotal << std::endl; 
    std::cout << "int total: " << floatTotal << std::endl; 
} 

我喜歡std::accumulate,雖然:

int main() { 
    const auto intVals = { 0, 1, 2 }; 
    const auto floatVals = { 0.F, 1.F, 2.F }; 

    const auto intTotal = std::accumulate(intVals.begin(), intVals.end(), 0); 
    const auto floatTotal = std::accumulate(floatVals.begin(), floatVals.end(), 0.0F); 

    std::cout << "float total: " << intTotal << std::endl; 
    std::cout << "int total: " << floatTotal << std::endl; 
} 
1

的實現涉及模板參數和使用STL(C++標準庫)的:

#include <iostream> 
#include <vector> 
#include <cstddef> 

template <typename T> 
T Sum(const std::vector<T>& a) { 
    T result = 0; 
    for (size_t i = 0; i < a.size(); ++i) { 
     result += a[i]; 
    } 
    return result; 
} 


int main() { 
    std::vector<int> intVals {0, 1, 2}; 
    std::vector<float> floatVals {0.F, 1.F, 2.F}; 

    int intTotal = Sum(intVals); 
    float floatTotal = Sum(floatVals); 

    std::cout << intTotal << std::endl; 
    std::cout << floatTotal << std::endl; 
} 

參見Live Demo

+0

你真的需要指定''還是編譯器可以推斷出vector intVals是由int組成的? – Ceros

+0

@Ceros你說得對,沒必要。 –