Container choice should follow how the data is used. Start with std::vector, then switch when another container matches the problem better.
std::array
std::array<T, N> stores a fixed number of elements:
std::array<int, 3> rgb{255, 128, 0};
Use it when the size is known at compile time and should not change.
Prefer std::array over raw arrays in beginner and intermediate code because it knows its size and works better with standard algorithms.
std::map
std::map<Key, Value> stores key-value pairs ordered by key:
std::map<std::string, int> counts;
counts["error"] += 1;
Use it when ordered iteration matters or when stable ordering is useful.
std::unordered_map
std::unordered_map<Key, Value> stores key-value pairs in a hash table:
std::unordered_map<std::string, int> counts;
counts["error"] += 1;
Use it for average fast lookup when ordering does not matter.
std::set
std::set<T> stores unique ordered values:
std::set<std::string> tags;
tags.insert("network");
tags.insert("storage");
Use a set when uniqueness is central and ordered iteration matters. If order does not matter, std::unordered_set may fit.
Choosing simply
Use this starting point:
std::vectorfor sequencesstd::stringfor owned textstd::arrayfor fixed-size sequencesstd::unordered_mapfor key lookup without orderstd::mapfor key lookup with orderstd::setwhen unique sorted values are the main point
Do not choose a container by habit alone. Ask what operations dominate: iteration, lookup, insertion, removal, ordering, or memory locality.
Avoid raw arrays by default
Raw arrays appear in older C++ and in low-level interfaces:
int values[3]{1, 2, 3};
They are not the best default for modern code. Prefer std::array or std::vector unless you have a specific reason.
What to carry forward
std::vectorremains the default sequence container- fixed-size data fits
std::array - lookup data often fits map or unordered map
- sets model uniqueness
- container choice depends on access pattern and constraints
- raw arrays are low-level tools, not beginner defaults
Next, you will learn iterators, algorithms, and invalidation.