Hold data and provide access to it. Random-access containers: - Allow accessing any element by index - arrays, vectors Sequential containers: - Allow accessing elements by sequence (not by index) - queues, stacks, linked lists - Require iterators to locate elements Containers Containers
17
Embed
Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Hold data and provide access to it.Random-access containers:- Allow accessing any element by
index- arrays, vectorsSequential containers:- Allow accessing elements by
sequence (not by index)- queues, stacks, linked lists- Require iterators to locate elements
ContainersContainers
In principle one can solve any problem with arrays or vectors.
In practice queues, stacks and linked lists are more convenient and more efficient performance-wise for select applications.
size() returns string size;<, >, <=, >=, !=, and == comparison operators;<< and >> stream I/O operators;+ Concatenates two string objects;[ ] index operator for character access.
string class (STL)string class (STL)
#include <vector>vector<int> v;v.push_back(1);
size(), capacity() returns vector size, capacity;insert() inserts element at specified pos;erase() removes element at specified pos;push_back() adds element at the end;pop_back() removes element at the end.begin() returns position of the first element;end() returns position of the last element.[] index operator
Generic vector class (STL)Generic vector class (STL)
vector is cool, but insert() and erase() operations operate in linear time – O(N).
What if we need to insert and remove quickly?
We can use linked list to insert and remove in constant time – O(1)!
size() returns list size;insert() / erase() inserts / removes element at specified pos;push_front() adds element at the beginning;push_back() adds element at the end;pop_front() removes element at the beginning;pop_back() removes element at the end;begin() / end() returns position of the first / last element;end() returns position of the last element.Iterator - iterator for the list ++ next element in the list -- previous element in the list * current element
Generic list Class (STL)Generic list Class (STL)
#include <list>using namespace std;
list<string> myList;
for ( list<string>::iterator pos = myList.begin(); pos != myList.end(); ++pos ){ // Current element string s = *myList;}
list Traversal Examplelist Traversal Example
// Prefix: ++iter;// Modifies self and returns the NEW valueiterator& operator++();
// Postfix: iter++;// Modifies self, but returns a copy of OLD self created
prior to the modificationiterator operator++(int);
for (; ; ++pos ) // More efficient (no copying)for (; ; pos++ ) // Equivalent to prefix++ in this loop
MyFunc(pos++); // Firtst MyFunc is called then pos++MyFunc(++pos); // First pos++ then MyFunc is called