Interview Question

vector vs list?

vector is contiguous and random-access; list is node-based and bidirectional.

💡 Concept ✅ Quick Revision 🧠 C++

Answer

std::vector stores elements contiguously, while std::list is a doubly linked list. • vector provides constant-time random access and usually strong cache locality. • list supports constant-time insertion or erasure at a known position without moving other elements. • vector is usually the default sequence container unless stable node positions or frequent interior splicing is required.

💡 C++ Example

std::vector<int> values{1, 2, 3}; int third = values[2]; std::list<int> nodes{1, 2, 3}; nodes.push_front(0);

⚡ Quick Revision

vector is contiguous and random-access; list is node-based and bidirectional.