OOP C3 Container
Collection objects are objects that can store an arbitrary number of other objects.
STL
Standard Template Library, STL: Part of the ISO Standard C++ Library and Data Structures and algorithms for C++.
Advantages
- Reduce development time: Data-structures already written and debugged.
- Code readability: Fit more meaningful stuff on one page.
- Robustness: STL data structures grow automatically.
- Portable, Maintainable code
- Easy
Components
The library
includes:
- A Pair class (pairs of anything,
e.g
int/int, int/char, etc) - Containers:
- Vector: expandable array
- Deque: expandable array at both ends
- List: double-linked
- Sets and Maps
- Basic Algorithms like sort, search, etc
All identifiers in library are in std
namespace
: using namespace std;
In summary, the three parts of STL are: Containers
, Algorithms
and
Iterators.
All Sequential Containers
- vector: variable array
- deque : dual-end queue
- list : double-linked-list
- forward_list : as it
- array : as “array”
- string : char. array
Vector
1 |
|
When declaring a collection,we need to specify the type of the collection itself vector
and the type of the elements that we plan to store in the collection.
Property
- increase its internal capacity as required: as more items are added, it simply makes enough room for them.
- keeps its own private count of how many items it is currently storing. Its size method returns the number of objects currently stored in it.
- maintains the order of items you insert into it. You can later retrieve them in the same order.
Operations
Constructors
1 |
|
Simple Methods
1 |
|
Iterators
1 |
|
Element access
1 |
|
Add/Remove/Find
1 |
|
List
Property
Same basic concepts as vector
and similar constructors, ability to compare lists
(== , != , < , <= , > , >= ) and ability to access front and back of list.
1 |
|
BUT additional ability on Add/Remove/Find.
1 |
|
Map
Maps: collections that contain pairs of values.
Pairs: consisting of a key and a value. Supplying a key
, a value
paired will be retrieved.
1 |
|
Iterator
1 |
|
as arguments
1 |
|
Loop
-
for-each loop:
1
2
3
4
5
6
7for(ElementType varName: array/vector/...Name) {
loop statements ...
}
for(int i : L) // auto
{
cout<<i<<" ";
}A
for-each
loop iterates over the elements of arrays, vectors, or any other data sets. It assigns the value of the current element to the variable iterator declared inside the loop.Pros and Cons
- Advantages
- Eliminates the possibility of errors
- Makes the code more readable.
- Easy to implement
- No pre-initialization of the iterator
- Disadvantages
- Cannot directly access the corresponding element indices
- Cannot traverse the elements in reverse order
- Doesn’t allow the user to skip any element as it traverses over each one
- Advantages
Typdef
- Annoying to type long names
- Simplify with typedef typedef.
- Easy to change implementation.
Sevral Pitfalls
Accessing an invalid
- use
push_back()
- Preallocate with
constructor
. - Reallocate with
reserve()
- Check
capacity()
Inadvertently inserting into map<> .
foo["bob"]==1
will silently created entry “bob” with value 1!
- Use
count()
to check for a key without creating a new entry.
Forget using empty() on list<>
empty()
is garuanted to be done in constant time, whilesize()
/count()
doesn’t !
Use invalid iterator
1 |
|