2016-04-18 9 views
1

Hier ist meine Übung Code .. Bücher Frage Code zu Vorlage Refactoring (mit STL)Wie man diesen Code mit STL umgestaltet?

ich einige Bücher gefunden und Google, aber ich tue es nicht

können Sie mir zeigen Beispiel setzen Sie sich bitte? ?

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

Beginnen Sie mit dem Ersetzen der RAW-Arrays durch '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

Wenn Ihr Buch sagt 'void main', ist es kein C++ Buch. –

Antwort

-1

Sie verwenden Vorlagen wie diese. Lesen Sie auch auf sie: http://www.cplusplus.com/doc/oldtutorial/templates/

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

} 

Sie verwenden es wie folgt aus:

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

Du T mit int in diesem Fall ersetzen

+0

In diesem Fall müssen Sie '' nicht angeben. Der Compiler kann es ableiten. Rufen Sie einfach 'doSomething (1, 1)' – DeathTails

+0

Ja gerade geändert thath @ DeathTails Dank – Ceros

5

Sie können 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

In diesem Beispiel Sie können Sum wie folgt sowohl SumInt und SumFloat mit einer Vorlage ersetzen:

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

Dann in Ihrem main() Sie

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

und der Compiler entweder die int oder float Version abhängig nennen kann auf die rufen Art des Arguments, das Sie an Sum liefern.

+0

Grundsätzlich ist die Idee, einen bestimmten Datentyp (in diesem Fall 'int' oder 'float') mit einem generischen zu ersetzen. Menschen verwenden oft "T" als ein Symbol für einen generischen Typ. – Logicrat

1

Hier gehen wir (eine einfache Vorlage für Ihre Summe (...) Funktion):

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

Ich mag std::accumulate, aber:

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

Eine Implementierung beteiligt Template-Parameter und Verwendung von STL (C++ Standard-Bibliothek):

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

Siehe Live Demo

+0

Müssen Sie wirklich die '' angeben oder der Compiler kann folgern, dass der Vektor 'intVals' aus' int' besteht? – Ceros

+0

@Ceros Sie haben Recht, es ist nicht notwendig. –

Verwandte Themen