5.STL Sequence Containers
5.STL Sequence Containers
Ali Malik
[email protected]
Game Plan
Recap
Stream wrapup
Overview of STL
Sequence Containers
std::vector
std::deque
Container Adapters
Announcements
Recap
stringstream
stringstream
Sometimes we want to be able to treat a string like a stream.
Useful scenarios:
int myInt;
double myDouble;
std::string myString;
stream >> myInt >> myDouble >> myString;
Simple IO
(OurSimpIO.pro)
Useful Aside
Structs
You can define your own mini-types that bundle multiple
variables together:
struct point {
int x;
int y;
};
point p;
p.x = 12;
p.y = 15;
Overview of STL
Overview of STL
Algorithms Functors/Lambdas
Iterators Adapters
Allocators Containers
Overview of STL
Algorithms Functors/Lambdas
Iterators Adapters
Allocators Containers
Where we are going...
Here is a program that generates a vector with random entries,
sorts it, and prints it, all in one go!
Examples:
● std::vector<T>
● std::list<T>
● std::deque<T>
std::vector<T>
std::vector<T>
A vector represents a sequence of elements of any type.
You specify the type when using the vector:
Create a vector with n copies of a value k Vector<int> v(n, k); vector<int> v(n, k);
Get the element at index i (verify that i is in int k = v.get(i); int k = v.at(i);
bounds) int k = v[i];
Create a vector with n copies of a value k Vector<int> v(n, k); vector<int> v(n, k);
Get the element at index i (verify that i is in int k = v.get(i); int k = v.at(i);
bounds) int k = v[i];
Create a vector with n copies of a value k Vector<int> v(n, k); vector<int> v(n, k);
Get the element at index i (verify that i is in int k = v.get(i); int k = v.at(i);
bounds) int k = v[i];
Create a vector with n copies of a value k Vector<int> v(n, k); vector<int> v(n, k);
Get the element at index i (verify that i is in int k = v.get(i); int k = v.at(i);
bounds) int k = v[i];
Write a program that reads a list of integers and finds the median.
Vector Median
(VecMedian.pro)
std::vector<T>
Some new stuff there:
std::sort(vec.begin(), vec.end());
std::vector<T>
Some new stuff there: This is a promise to the
compiler that this variable
won’t change.
const int kNumInts = 5;
std::sort(vec.begin(), vec.end());
std::vector<T>
Some new stuff there:
std::sort(vec.begin(), vec.end());
std::sort(vec.begin(), vec.end());
3 1 4 1 5
0th index
Why is push_front slow?
Suppose push_front existed and we used it.
3 1 4 1 5
0th index
Why is push_front slow?
Suppose push_front existed and we used it.
vec.push_front(7);
7
3 1 4 1 5
0th index
Why is push_front slow?
Suppose push_front existed and we used it.
vec.push_front(7);
7 Need to shift these
elements up to make space
in the 0th position.
3 1 4 1 5
0th index
Why is push_front slow?
Suppose push_front existed and we used it
vec.push_front(7);
7 Need to shift these
elements up to make space
in the 0th position.
3 1 4 1 5
0th index
Why is push_front slow?
Suppose push_front existed and we used it
vec.push_front(7);
7 Need to shift these
elements up to make space
in the 0th position.
3 1 4 1 5
0th index
Why is push_front slow?
Suppose push_front existed and we used it
vec.push_front(7);
7 Need to shift these
elements up to make space
in the 0th position.
3 1 4 1 5
0th index
Why is push_front slow?
Suppose push_front existed and we used it
vec.push_front(7);
7 Need to shift these
elements up to make space
in the 0th position.
3 1 4 1 5
0th index
Why is push_front slow?
Suppose push_front existed and we used it
vec.push_front(7);
7 Need to shift these
elements up to make space
in the 0th position.
3 1 4 1 5
0th index
Why is push_front slow?
Suppose push_front existed and we used it
vec.push_front(7);
7 Need to shift these
elements up to make space
in the 0th position.
3 1 4 1 5
0th index
Why is push_front slow?
Suppose push_front existed and we used it
vec.push_front(7);
7 Need to shift these
elements up to make space
in the 0th position.
3 1 4 1 5
0th index
Why is push_front slow?
Suppose push_front existed and we used it
vec.push_front(7);
7 Need to shift these
elements up to make space
in the 0th position.
3 1 4 1 5
0th index
Why is push_front slow?
Suppose push_front existed and we used it
vec.push_front(7);
7 Need to shift these
elements up to make space
in the 0th position.
3 1 4 1 5
0th index
Why is push_front slow?
Suppose push_front existed and we used it
vec.push_front(7);
7 Need to shift these
elements up to make space
in the 0th position.
3 1 4 1 5
0th index
Why is push_front slow?
Suppose push_front existed and we used it
vec.push_front(7);
7
3 1 4 1 5
0th index
Why is push_front slow?
Suppose push_front existed and we used it
vec.push_front(7);
7
Now we can insert the
new element.
3 1 4 1 5
0th index
Why is push_front slow?
Suppose push_front existed and we used it
vec.push_front(7);
7 3 1 4 1 5
0th index
Why is push_front slow?
...
7 3 1 4 1 5
0th index
Why is push_front slow?
Let’s get a sense of the difference:
Insertion Speed
(InsertionSpeed.pro)
Why is push_front slow?
The results:
Why is push_front slow?
A vector is the prime tool of choice in most applications!
● Fast
● Lightweight
● Intuitive
and also…
Deque Speed
(DequeSpeed.pro)
std::deque<T>
The results:
std::deque<T>
The results:
Same scale as
previous graph
std::deque<T>
The results:
NULL
How does std::deque<T> work?
There is no single specific implementation of a deque, but one
common one might look like this:
NULL
3 1 2 6 5
4 1 5 9
How does std::deque<T> work?
There is no single specific implementation of a deque, but one
common one might look like this:
deq.push_front(7);
NULL
3 1 2 6 5
4 1 5 9
How does std::deque<T> work?
There is no single specific implementation of a deque, but one
common one might look like this:
deq.push_front(7);
NULL
3 1 2 6 5
4 1 5 9
How does std::deque<T> work?
There is no single specific implementation of a deque, but one
common one might look like this:
deq.push_front(7);
NULL
7 3 1 2 6 5
4 1 5 9
How does std::deque<T> work?
There is no single specific implementation of a deque, but one
common one might look like this:
NULL
7 3 1 2 6 5
4 1 5 9
How does std::deque<T> work?
There is no single specific implementation of a deque, but one
common one might look like this:
deq.push_back(3);
NULL
7 3 1 2 6 5
4 1 5 9
How does std::deque<T> work?
There is no single specific implementation of a deque, but one
common one might look like this:
deq.push_back(3);
NULL
7 3 1 2 6 5
4 1 5 9
How does std::deque<T> work?
There is no single specific implementation of a deque, but one
common one might look like this:
deq.push_back(3);
NULL
7 3 1 2 6 5 3
4 1 5 9
How does std::deque<T> work?
There is no single specific implementation of a deque, but one
common one might look like this:
NULL
7 3 1 2 6 5 3
4 1 5 9
How does std::deque<T> work?
There is no single specific implementation of a deque, but one
common one might look like this:
deq.push_back(5);
NULL
7 3 1 2 6 5 3
4 1 5 9
How does std::deque<T> work?
There is no single specific implementation of a deque, but one
common one might look like this:
deq.push_back(5);
7 3 1 2 6 5 3
4 1 5 9
How does std::deque<T> work?
There is no single specific implementation of a deque, but one
common one might look like this:
deq.push_back(5);
7 3 1 2 6 5 3
4 1 5 9
How does std::deque<T> work?
There is no single specific implementation of a deque, but one
common one might look like this:
deq.push_back(5);
7 3 1 2 6 5 3
4 1 5 9
How does std::deque<T> work?
There is no single specific implementation of a deque, but one
common one might look like this:
deq.push_back(5);
7 3 1 2 6 5 3
4 1 5 9
How does std::deque<T> work?
There is no single specific implementation of a deque, but one
common one might look like this:
7 3 1 2 6 5 3
4 1 5 9
Wait a minute...
Question
If deque can do everything a vector can do and also has a fast
push_front...
Vector vs Deque
(VecDeqSpeed.pro)
Downsides of std::deque<T>
The results:
Which to Use?
13
41
12
stack
Container Adapters
Recall stacks and queues:
push
13
41
12
stack
Container Adapters
Recall stacks and queues:
13
41
12
stack
Container Adapters
Recall stacks and queues:
pop
13
41
12
stack
Container Adapters
Recall stacks and queues:
13
41
12
stack
Container Adapters
Recall stacks and queues:
16
13
11
41
12 back
stack queue
Container Adapters
Recall stacks and queues:
16
13
11
41
5
12 back
16
13
11
41
5
12 back
stack queue
Container Adapters
Recall stacks and queues: pop_front
16
13
11
41
5
12 back
stack queue
Container Adapters
Recall stacks and queues:
16
13
11
41
5
12 back
stack queue
Container Adapters
How can we implement stack and queue using the containers we have?
Stack:
Just limit the functionality of a vector/deque to only allow push_back and
pop_back.
Queue:
Just limit the functionality of a deque to only allow push_back and
pop_front.