Advertisement
Intermediate C++, Ranges & Views

What is the difference between a range and a container?

Short answer

A container owns its elements and manages their storage (vector, map). A range is any type providing begin()/end()—which includes containers, views, arrays, and generated sequences. Views are ranges that don't own data.

Save C++ mock interview
Advertisement
AModel answer
A container owns its elements and manages their storage (vector, map). A range is any type providing begin()/end()—which includes containers, views, arrays, and generated sequences. Views are ranges that don't own data. So all containers are ranges, but not all ranges are containers.
Real-world example
A function templated on a range accepts vectors, arrays, and lazy views uniformly.
Likely follow-ups
  • Do all ranges own their elements? | Is a container a range?
Related topics