# Posted By

jimfred on 11/19/08

# Statistics

Viewed 261 times
Favorited by 0 user(s)

# C++ template quick reference

/ Published in: C++
`VECTOR (Variable sized array/stack with built in memory allocation) vector<int> a(10);        // a[0]..a[9] are int (default size is 0)a.size();                 // Number of elements (10)a.push_back(3);           // Increase size to 11, a[10]=3a.back()=4;               // a[10]=4;a.pop_back();             // Decrease size by 1a.front();                // a[0];a[20]=1;                  // Crash: not bounds checkeda.at(20)=1;               // Like a[20] but throws out_of_range()for (vector<int>::iterator p=a.begin(); p!=a.end(); ++p)  *p=0;                   // Set all elements of a to 0vector<int> b(a.begin(), a.end());  // b is copy of avector<T> c(n, x);        // c[0]..c[n-1] init to xT d[10]; vector<T> e(d, d+10);      // e is initialized from d DEQUE (array/stack/queue)deque<T> is like vector<T>, but also supports: a.push_front(x);          // Puts x at a[0], shifts elements toward backa.pop_front();            // Removes a[0], shifts toward front UTILITY (Pair) pair<string, int> a("hello", 3);  // A 2-element structa.first;                  // "hello"a.second;                 // 3 MAP (associative array) map<string, int> a;       // Map from string to inta["hello"]=3;             // Add or replace element a["hello"]for (map<string, int>::iterator p=a.begin(); p!=a.end(); ++p)  cout << (*p).first << (*p).second;  // Prints hello, 3a.size();                 // 1`