C++ How to set starting values for vector?

You can set the starting values for a vector in C++ in a few different ways.

  1. Using initialization list syntax:
refer ‮:ot‬theitroad.com
std::vector<int> v {1, 2, 3, 4, 5};

In this example, we initialize a vector v with 5 elements: 1, 2, 3, 4, and 5.

  1. Using the assign() method:
std::vector<int> v;
v.assign({1, 2, 3, 4, 5});

In this example, we first create an empty vector v, and then use the assign() method to set its starting values to 1, 2, 3, 4, and 5.

  1. Using the resize() method:
std::vector<int> v;
v.resize(5, 0);

In this example, we first create an empty vector v, and then use the resize() method to set its size to 5 and its starting values to 0.

  1. Using the push_back() method:
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);

In this example, we first create an empty vector v, and then use the push_back() method to add the values 1, 2, 3, 4, and 5 one by one to the vector.

All of these methods allow you to set the starting values for a vector in C++, depending on your specific use case and preference.