Templates look a little funny at first, but they're not that much different from normal classes once you get used to them. The only strange part is the angle brackets: a vector of Car is declared using the syntax vector<Car>. The typedef syntax is used for convenience: it creates easy-to-read synonyms such as CarList. #include <vector> <-- 1 #include <string> <-- 2 #include <algorithm> using namespace std; #include "Car.hpp" <-- 3 typedef vector<Car> CarList; <-- 4 typedef vector<string> StringList; <-- 5 int main() { CarList x; <-- 6 Car a, b, c; x.push_back(a); <-- 7 x.push_back(b); x.push_back(c); // ... StringList y; <-- 8 y.push_back("Foo"); <-- 9 y.push_back("Bar"); y.push_back("Baz"); sort(y.begin(), y.end()); <-- 10 // ... }
This sample code creates two vector objects: x is a vector of Car objects and y is a vector of string objects. This is analogous to creating two C-like arrays (Car x[3]; and string y[3];), but vector objects are more flexible, they can grow to an arbitrary size, they are safer, and they have a lot more services associated with them. See FAQ 28.13. UML uses the following notation to show a template vector along with instantiations of that template vector<Car> and vector<string>. |