Open zwkcoding opened 5 years ago
C++ Tips:
vector::resize() vs vector::reserve()
TLTR:
If you have initial estimate, then reserve()
that estimate.Or, just let the vector
do it's thing.
refer to sf:Choice between vector::resize() and vector::reserve()
vector::resize() vs vector::assign()
TLTR: assign()
mean replace, resize()
mean expanding.
refer to sf:STL vector: resize() and assign()
Keywords: Queen->FIFO
Colusion :+1:
class MyQueue { private: // store elements vector data;
// a pointer to indicate the start position int p_start;
public: MyQueue() {p_start = 0;} / Insert an element into the queue. Return true if the operation is successful. */ bool enQueue(int x) { data.push_back(x); return true; } /* Delete an element from the queue. Return true if the operation is successful. / bool deQueue() { if (isEmpty()) { return false; } p_start++; return true; }; / Get the front item from the queue. */ int Front() { return data[p_start]; }; /* Checks whether the queue is empty or not. / bool isEmpty() { return p_start >= data.size(); } };
int main() { MyQueue q; q.enQueue(5); q.enQueue(3); if (!q.isEmpty()) { cout << q.Front() << endl; } q.deQueue(); if (!q.isEmpty()) { cout << q.Front() << endl; } q.deQueue(); if (!q.isEmpty()) { cout << q.Front() << endl; } }
Don't reinvent the wheel! refer to @liuyubobobo/Play-Leetcode-Explore