Saturday, August 2, 2014

[C++] Data structures and their common operators


vector
stack
queue
Deque
(Double ended queue)
Priority_queue
Access
x=v.front()
x=v.back()
x=s.top()
x=q.front()
x=dq.front()
x=dq.back()
x=pq.top()
Add
v.insert(itr, x)
v.push_back(x)
v.emplace(itr, x)
v.emplace_back(x)
s.emplace(x)
s.push(x)
q.emplace(x)
q.push(x)
dq.emplace(itr, x)
dq.emplace_back(x)
dq.emplace_front(x)
dq.push_back(x)
dq.push_front(x)
dq.insert(itr,x)
pq.emplace(x)
pq.push(x)
Remove
v.clear()
v.erase(itr)
v.pop_back()
s.pop()
q.pop()
dq.clear()
dq.erase(itr)
dq.pop_back()
dq.pop_front()
pq.pop()
Check empty
v.empty()
s.empty()
q.empty()
dq.empty()
pq.empty()
Check size
x=v.size()
x=s.size()
x=q.size()
dq.size()
pq.size()

Vector vs. Deque:

Both vector and deque provide a very similar interface, but internally they work in quite different ways: vector uses a single array that needs occasionally re-allocation for growth; deque has its elements scattered in different chunk of storage, with the container keeping the necessary information for direct accesses to any element in constant time - through iterators.

No comments:

Post a Comment