vector int or pair int,int, which is better? – Code Example

Total
0
Shares

In this article we will understand which is better – vector<vector<int>> or vector<pair<int, int>>.

  1. vector has dynamic size so end up using heap but pair is fixed so there is no overhead.
  2. Pair has better cache locality and fewer indirections.
  3. Dynamic memory allocation is slow so vector memory allocation is slow.
  4. Pairs have consecutive memory allocations which is faster to access for modern computer systems, while vectors may have distributed memory location.

This concludes that pair<int, int> is better choice than vector<int>