Class Two
Containers¶
- Also Known As: Collection
- STL: Standard Template Library
- Reduce development time and increase efficiency
Data-structures are already written and debugged
Why STL?¶
- Code readability
- Robustness
- Portable, maintainable, and easy
C++ Standard Library¶
- a pair class:
-
Containers:
- vector
- deque: double-ended queue
- list: doubly linked list
- set:
- map: dictionary
- vector
-
Basic Algorithms, sort, search
- all identifiers are in the std namespace
Examples¶
-
vector
-
Generic Classes:
vector<int> v;vector<string> notes
Have to specify two types: the type of the collection itself and the type of the elements that we plan to store in the collection -
Constructors:
-
vector<int> v; vector<int> v1(v): two vectors,v1is a copy ofv -
Simple Methods:
v.size(): number of itemsv.empty(): whether the vector is empty
==,!=,>,<,>=,<=
v.swap(v1): swap the contents of two vectors -
iterators:
vector<int>::iterator it
it=v.begin(): iterator of the first position
it=v.end(): iterator of the last position -
Element Access:
v.at(index)
v[index]
v.front(),v.back() -
Add/Remove/Find:
v.push_back(e)
v.pop_back()
v.insert(pos,e)
v.erase(pos)
v.clear()
v.find(first, last, item) -
Preallocate Memory:
List¶
- same basic concepts as vector, but double linked list
list<int> l;l.push_back(e),l.push_front(e)l.pop_back(),l.pop_front()