devxlogo

Specifying the Size of a Vector During Construction

Specifying the Size of a Vector During Construction

It is possible to specify the requested storage size of a vector during construction like this:

 #include using namespace std;int main() {  vector  vi(1000); //initial storage for 1000 int's  //vi contains 1000 elements initialized by int::int()}

Remember that vector::reserve() allocates raw memory without initializing it. The constructor, on the other hand, initializes the allocated elements by invoking their default constructor. It is possible to specify a different initializer value:

 vector  vi(1000, 4); //initialize  all 1000 int's with 4
See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist