Industrielle Fertigung
Industrielles Internet der Dinge | Industrielle Materialien | Gerätewartung und Reparatur | Industrielle Programmierung |
home  MfgRobots >> Industrielle Fertigung >  >> Industrial programming >> C Sprache

C++ Templates: Generische Programmierung meistern

Templates sind Basis generischer Programmierung – Code unabhängig von konkreten Typen schreiben.

vector, Iteratoren, Algorithmen) nutzen Templates. Ein Container-Definition (z. B. vector), aber viele Instanziierungen: vector<int>, vector<string>.

Funktions-Templates

Allgemeine Form:

template <class type> ret-type func-name(parameter-list) {
    // Funktionskörper
}

type = Platzhalter für Datentyp, im Funktionskörper nutzbar.

Beispiel: Maximum zweier Werte

#include <iostream>
#include <string>
using namespace std;
template <typename T>
inline T const& Max(T const& a, T const& b) {
    return a < b ? b : a;
}
int main() {
    int i=39, j=20;
    cout << "Max(i,j): " << Max(i,j) << endl;
    double f1=13.5, f2=20.7;
    cout << "Max(f1,f2): " << Max(f1,f2) << endl;
    string s1="Hello", s2="World";
    cout << "Max(s1,s2): " << Max(s1,s2) << endl;
    return 0;
}

Ausgabe:

Max(i,j): 39
Max(f1,f2): 20.7
Max(s1,s2): World

Klassen-Templates

Allgemeine Form:

template <class type> class class-name { ... }

type = Platzhalter, bei Instanziierung festgelegt. Mehrere generische Typen per Komma-Liste.

Beispiel: Generischer Stack

#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <stdexcept>
using namespace std;

template <class T>
class Stack {
private:
    vector<T> elems;
public:
    void push(T const&);
    void pop();
    T top() const;
    bool empty() const { return elems.empty(); }
};

template <class T>
void Stack<T>::push(T const& elem) { elems.push_back(elem); }

template <class T>
void Stack<T>::pop() {
    if(elems.empty()) throw out_of_range("Stack<>::pop(): empty stack");
    elems.pop_back();
}

template <class T>
T Stack<T>::top() const {
    if(elems.empty()) throw out_of_range("Stack<>::top(): empty stack");
    return elems.back();
}

int main() {
    try {
        Stack<int> intStack;
        Stack<string> stringStack;
        intStack.push(7);
        cout << intStack.top() << endl;  // 7
        stringStack.push("hello");
        cout << stringStack.top() << endl; // hello
        stringStack.pop();
        stringStack.pop();  // Exception: empty stack
    } catch(exception const& ex) {
        cerr << "Exception: " << ex.what() << endl;
        return -1;
    }
    return 0;
}

Ausgabe:

7
hello
Exception: Stack<>::pop(): empty stack

C Sprache

  1. C++-Datentypen verstehen: Grundlagen, Modifizierer und Best Practices
  2. C++-Operatoren meistern: Ein vollständiger Leitfaden mit praktischen Beispielen
  3. C++-Kommentare: Best Practices für lesbaren, wartbaren Code
  4. C++-Konstruktoren verstehen: Standard-, Parameter- und Kopierkonstruktor erklärt
  5. C++ Programmiersprache: Überblick & Kernfeatures
  6. C++ Konstanten & Literale: Typen, Syntax & Best Practices
  7. C++ Operatoren: Typen, Nutzung & Best Practices
  8. C++ Numerische Typen: Praktischer Leitfaden
  9. C++ Referenzen: Konzepte & Unterschiede zu Pointern
  10. C++ Präprozessor: Direktiven formen Ihren Code