10/17/2019 - 2:22 AM

Init all vector elements to all same value


#include <iostream>
#include <string>
#include <vector>

using namespace std;
template <class T>
void print_v(const T& v, string msg = "") {
  cout << msg;

  for (auto i : v) {
    cout << i << " ";
  }
  cout << endl;
}

void init_vec_to_same_value() {
  auto size = 5;
  vector<int> defaultVec(size);
  print_v(defaultVec, "Default value in vector is zero\n");

  vector<int> initVec(size, 1);
  print_v(initVec, "Value can be set when creating vector\n");

  vector<int> assignVec(size);
  auto newSize = size * 2;
  assignVec.assign(newSize, 10);
  print_v(assignVec,
          "assign function changes both the size and value of vector\n");
}

int main() { init_vec_to_same_value();}