During a recent interview, I suggested using vector<pair<int,int>>
over vector<vector<int>>
since we only wanted to store two values for every entry in the vector. I said something to the tune of "we should use vector<pair<int,int>>
over vector<vector<int>>
since the latter is heavier than the former".
After the coding session was over, they said it was a good idea to use pair over a vector and asked me to elaborate what I meant by "heavier" earlier. I wasn't able to elaborate, unfortunately. Yes, I know we can enter only two values in a pair but many more in a vector and that vector gets resized automatically when its size==capacity, etc. but how should I have answered their question - why specifically was using vector<pair<int,int>>
better than vector<vector<int>>
? What extra things are done in the latter case?
vector<vector<int>>
you need a dynamic memory allocation for each pair, in addition to the allocation for the outer vector. Dynamic memory allocation is not generally fast, and the result can have poor memory locality (consecutive elements may not be near each other in memory). Modern computer architecture likes to access objects which are near other objects it has accessed recently, and can run orders of magnitude faster when that is the case. Withvector<pair<int,int>>
all of the elements are consecutive, which will help when you have to work on the whole container. – Arrestsizeof(std::pair<int, int>) < sizeof(std::vector<int>)
as well, but this is not so important compared to the time overhead of dynamic allocation and the memory locality issues mentioned in the comments above. – Expedientialsizeof(std::vector<int>)
withsizeof(std::pair<int, int>)
, to talk about howstd::vector
has a number of members it uses to manage memory whilestd::pair
is essentially a simple data structure that contains two members and does no memory management. – Companionway