What is the easiest way to initialize a vector in C++ with hardcoded elements, similar to array initialization?

int a[] = {10, 20, 30}; I currently use:

std::vector ints; ints.push_back(10); ints.push_back(20); ints.push_back(30); Is there a more concise or modern approach to initialize vector C++ like this?

Been working with C++ for a while, and honestly, I used to take the long route with push_back everywhere. But a few years back, I stumbled on initializer lists and never looked back. If you want to initialize vector C++ the clean way, just do this:

std::vector<int> ints = {10, 20, 30};

Reads just like an array and is much more readable. Way easier to manage, especially in larger codebases.

Right there with you, @miro.vasil. Once I started using C++11 regularly, brace-initialization became my default. It’s not just neater — it’s modern, type-safe, and expressive. So if I’m setting up config values or writing tests, I go with:

auto ints = std::vector<int>{10, 20, 30};

This is probably the most intuitive way to initialize vector C++ these days. It just feels right, especially when you’re working with complex types or templates.

Great points above, I’ve been using C++ for over a decade, and clarity has always been key for me. Whenever possible, I try to initialize vector C++ at the point of declaration. That way, I know exactly what I’m working with. My go-to looks like this:

std::vector<int> ints{10, 20, 30};

And if I’m just filling with repeated values, this shortcut comes in handy too:

std::vector<int> ints(3, 10); // [10, 10, 10]

Really comes down to the context — initializer lists for specific data, constructor overloads for patterns. Either way, both are way cleaner than manually pushing values.