0
0
CppHow-ToBeginner · 3 min read

How to Initialize Vector in C++: Syntax and Examples

In C++, you can initialize a std::vector using different methods such as empty initialization, with a fixed size, or with a list of values using initializer lists. For example, std::vector v = {1, 2, 3}; creates a vector with three elements.
📐

Syntax

Here are common ways to initialize a std::vector in C++:

  • Empty vector: Creates a vector with no elements.
  • Fixed size vector: Creates a vector with a specified number of default-initialized elements.
  • Fixed size with value: Creates a vector with a specified number of elements, each initialized to a given value.
  • Initializer list: Creates a vector with elements from a list of values.
cpp
std::vector<int> v1;               // empty vector
std::vector<int> v2(5);            // vector with 5 default-initialized ints (0)
std::vector<int> v3(5, 10);        // vector with 5 ints, each 10
std::vector<int> v4 = {1, 2, 3};   // vector with elements 1, 2, 3
💻

Example

This example shows how to create vectors using different initialization methods and prints their contents.

cpp
#include <iostream>
#include <vector>

int main() {
    std::vector<int> emptyVec;
    std::vector<int> fixedSizeVec(3);
    std::vector<int> fixedValueVec(4, 7);
    std::vector<int> listVec = {10, 20, 30};

    std::cout << "emptyVec size: " << emptyVec.size() << "\n";

    std::cout << "fixedSizeVec elements: ";
    for (int n : fixedSizeVec) std::cout << n << ' ';
    std::cout << "\n";

    std::cout << "fixedValueVec elements: ";
    for (int n : fixedValueVec) std::cout << n << ' ';
    std::cout << "\n";

    std::cout << "listVec elements: ";
    for (int n : listVec) std::cout << n << ' ';
    std::cout << "\n";

    return 0;
}
Output
emptyVec size: 0 fixedSizeVec elements: 0 0 0 fixedValueVec elements: 7 7 7 7 listVec elements: 10 20 30
⚠️

Common Pitfalls

Common mistakes when initializing vectors include:

  • Forgetting to include <vector> header.
  • Using parentheses instead of braces for initializer lists, which can cause confusion.
  • Assuming default values are uninitialized; for int, default is zero.
cpp
/* Wrong: This creates a vector with one element (5), not 5 elements */
std::vector<int> wrongVec(5); // creates 5 zeros
std::vector<int> wrongVec2{5}; // creates vector with one element 5

/* Right: To create 5 elements all with value 5 */
std::vector<int> rightVec(5, 5);
📊

Quick Reference

Summary of vector initialization methods:

Initialization MethodCode ExampleDescription
Empty vectorstd::vector v;Creates an empty vector with zero elements.
Fixed sizestd::vector v(5);Creates vector with 5 default-initialized elements (0 for int).
Fixed size with valuestd::vector v(5, 10);Creates vector with 5 elements, each set to 10.
Initializer liststd::vector v = {1, 2, 3};Creates vector with elements 1, 2, and 3.

Key Takeaways

Use std::vector<T> v; for an empty vector.
Use std::vector<T> v(size); to create a vector with default values.
Use std::vector<T> v(size, value); to fill with a specific value.
Use initializer lists {} to set elements directly.
Remember to include <vector> and <iostream> for usage and output.