STL ist die sogenannte Standard Template Library. Diese enthält viele nützliche Funktionen um Daten zu verwalten.
Besonders gebräuchlich ist der STL vector
Erzeugen:
Wichtige Zugriffsfunktionen:
// STL vec test // compile with // g++ -std=c++11 -o testVector11 testVector11.C // #include <vector> // vector headers #include <iostream> using namespace std; int main() { vector<double> vx = { 0.1, 2.5, 8.1, 27.2 }; // direct initialization only in C++11 for (int i = 0; i < vx.size(); i++ ) { // classical loop, mit Index cout << vx[i] << endl; } // or in C++11 for (auto a: vx) { cout << a << endl; } vector<double> v; // empty <double> vector double x; while ( cin >> x ) { // read input until EOF v.push_back(x); // append in vector } // classical loop, mit Index for (int i = 0; i < v.size(); i++ ) { cout << v[i]; } }
Weiteres siehe letztes Kapitel.
Man kann mit den STL vector auch höherdimensionale Listen abbilden, allerdings ist die Syntax etwas sperrig:
// multi-dim vector as matrix // int matrix with 3x4 elements vector <vector <int> > m = { { 1, 0, 12, -1}, { 7, -3, 2, 5}, { -5, -2, 2, 9}}; // direct initialization} // access via cout << m[0][0] // 1. element cout << m[2][3] // last element // Matrix with 5 x 10 elements vector <vector <double>> d( 5, vector<double>(10)) ;
Genau genommen ist es ein vector von vector of int/double/..., letztere können auch unterschiedlich lang sein, z.B. Dreiecks-Matrix.