C++ Vector: How to Add Elements

To add elements to a vector, you can use one of the following methods:

  1. append()
  2. append()
std::vector<int> vec;
vec.push_back(10);
  1. Add the specified element to a certain location.
  2. – add in
std::vector<int> vec = {1, 2, 3};
vec.insert(vec.begin() + 1, 5);
  1. Add an element to the end
  2. Add an element to the end of a container
  3. Append or add to the end.
std::vector<std::string> vec;
vec.emplace_back("hello");
bannerAds