Here we will see some basic operations of array data structure. These operations are −
- Traverse
- Insertion
- Deletion
- Search
- Update
The traverse is scanning all elements of an array. The insert operation is adding some elements at given position in an array, delete is deleting element from an array and update the respective positions of other elements after deleting. The searching is to find some element that is present in an array, and update is updating the value of element at given position. Let us see one C++ example code to get better idea.
Example
#include<iostream> #include<vector> using namespace std; main(){ vector<int> arr; //insert elements arr.push_back(10); arr.push_back(20); arr.push_back(30); arr.push_back(40); arr.push_back(50); arr.push_back(60); for(int i = 0; i<arr.size(); i++){ //traverse cout << arr[i] << " "; } cout << endl; //delete elements arr.erase(arr.begin() + 2); arr.erase(arr.begin() + 3); for(int i = 0; i<arr.size(); i++){ //traverse cout << arr[i] << " "; } cout << endl; arr[0] = 100; //update for(int i = 0; i<arr.size(); i++){ //traverse cout << arr[i] << " "; } cout << endl; }
Output
10 20 30 40 50 60 10 20 40 60 100 20 40 60