Search code examples
c++stdvectorstdmaplarge-data

Which vector and map, uses less memory (large set of data and unknown size)


I wonder which container uses less memory between std::map and std::vector with a large set of data.

Loads of posts talk about efficiency, and my priority is not efficiency but memory consumption. So, if we don't know the number of our data (in my case can be over 12,000,000 entries, every entry is a string of 20 characters), is map really better than vector?


Solution

  • Depends on the problem you are solving. Basically , the std::vector uses a row memory(as your data is big, make sure you have one), but the std::map can take each node from separate parts of the memory. Opposite, std::map uses more memory for the same data because the pointer manipulation between nodes.