Insert an Element into an Array in C++
Inserting elements into an array is a fundamental operation in C++ programming, crucial for dynamic array management and data manipulation. In this article, we'll explore various methods to insert an element into an array, providing example programs, their outputs, and explanations. So without further ado, let's get started with method number one.
Method 1: Using Vector for Dynamic Array
The std::vector class in C++ offers a convenient way to insert elements into an array dynamically. Vectors automatically handle resizing and provide flexibility in managing array size.
#include <iostream>
#include <vector>
int main() {
std::vector<int> myArray = {1, 2, 3, 4, 5};
// Inserting element 10 at index 2
myArray.insert(myArray.begin() + 2, 10);
// Displaying the modified array
for (int num : myArray) {
std::cout << num << " ";
}
return 0;
}
1 2 10 3 4 5
The insert method is used to insert the specified element (10 in this case) at the specified index (myArray.begin() + 2), resulting in the modified array.
Method 2: Using std::copy for Static Array
For static arrays, the std::copy algorithm along with pointer manipulation can be employed to insert elements efficiently.
#include <iostream>
#include <algorithm>
int main() {
int myArray[] = {1, 2, 3, 4, 5};
int size = sizeof(myArray) / sizeof(myArray[0]);
// Inserting element 10 at index 2
int indexToInsert = 2;
int elementToInsert = 10;
// Creating a new array with space for the additional element
int newArray[size + 1];
// Copying elements before the insertion point
std::copy(myArray, myArray + indexToInsert, newArray);
// Inserting the new element
newArray[indexToInsert] = elementToInsert;
// Copying elements after the insertion point
std::copy(myArray + indexToInsert, myArray + size, newArray + indexToInsert + 1);
// Displaying the modified array
for (int num : newArray) {
std::cout << num << " ";
}
return 0;
}
1 2 10 3 4 5
This program creates a new array, copies elements before and after the insertion point, and inserts the specified element (10 in this case) at the desired index.
Method 3: Inserting by User Input
Allowing users to input the array size, elements, index, and the element to insert provides a practical example of array manipulation based on user requirements.
#include <iostream>
#include <algorithm>
int main() {
int size;
// Getting the size of the array from the user
std::cout << "Enter the size of the array: ";
std::cin >> size;
int myArray[size];
// Getting elements from the user
std::cout << "Enter " << size << " elements:\n";
for (int i = 0; i < size; ++i) {
std::cout << "Element " << i + 1 << ": ";
std::cin >> myArray[i];
}
int indexToInsert, elementToInsert;
// Getting the index and element to insert
std::cout << "Enter the index to insert: ";
std::cin >> indexToInsert;
std::cout << "Enter the element to insert: ";
std::cin >> elementToInsert;
// Creating a new array with space for the additional element
int newArray[size + 1];
// Copying elements before the insertion point
std::copy(myArray, myArray + indexToInsert, newArray);
// Inserting the new element
newArray[indexToInsert] = elementToInsert;
// Copying elements after the insertion point
std::copy(myArray + indexToInsert, myArray + size, newArray + indexToInsert + 1);
// Displaying the modified array
std::cout << "Modified array after insertion:\n";
for (int num : newArray) {
std::cout << num << " ";
}
return 0;
}
Enter the size of the array: 5 Enter 5 elements: Element 1: 1 Element 2: 2 Element 3: 3 Element 4: 4 Element 5: 5 Enter the index to insert: 2 Enter the element to insert: 10 Modified array after insertion: 1 2 10 3 4 5
This program allows the user to specify the size of the array, input the elements, and then enter the index and element they want to insert. The new array is then displayed after the insertion.
Sharing is stylish! Gift your friends this awesome read!