how to add elements in array c++ code example

Example 1: how to append an element to an array in cpp

std::vector<std::string> x = {"a", "b", "c"};
x.push_back("d");

Example 2: how to add elements in an array in for loop c++

int * myVar = new int [5];
for (int i = 0; i < 5; i++)
	myVar[i] = i

// 0 1 2 3 4 myVar in the array 

// delete stuff *optional*
delete [] myVar;
myVar = NULL; 

// free up memory declared on the stack

Example 3: insert element in array c++

#include <iostream>

using namespace std

int main() {
	int Array[] = {0}
    for (int i = 1; i < 15; i++) {
       	cin >> Array[i];
    };
    return 0;
}

Tags:

Cpp Example