├── README.md └── Vectors operation /README.md: -------------------------------------------------------------------------------- 1 | # Vectors-using-CPP 2 | -------------------------------------------------------------------------------- /Vectors operation: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | vector y = {1, 2, 3, 4, 5, 6, 7}; // Initialize vector 6 | 7 | // Printing initial elements in the vector 8 | for(int i = 0; i < y.size(); i++) { 9 | cout << i << "->" << y[i] << endl; 10 | } 11 | 12 | // Adding elements to the vector 13 | y.push_back(8); 14 | y.push_back(9); 15 | y.push_back(11); 16 | y.push_back(10); 17 | 18 | cout << "After inserting elements into the vector.....," << endl; 19 | 20 | // Printing elements after insertion 21 | for(int i = 0; i < y.size(); i++) { 22 | cout << i << "->" << y[i] << endl; 23 | } 24 | 25 | // Removing the last element 26 | y.pop_back(); 27 | 28 | cout << "After popping the last element from the vector.....," << endl; 29 | 30 | // Printing elements after pop_back 31 | for(int i = 0; i < y.size(); i++) { 32 | cout << i << "->" << y[i] << endl; 33 | } 34 | y.erase(y.begin() + 2); // Removes the element at index 2 35 | y.erase(y.begin() + 1, y.begin() + 3); // Removes elements from index 1 to 2 (range) 36 | 37 | y.insert(y.begin() + 2, 13); // Inserts 99 at index 2 38 | y.insert(y.begin(), 3, 14); // Inserts 50 three times at the beginning 39 | 40 | for(int i = 0; i < y.size(); i++) { 41 | cout << i << "->" << y[i] << endl; 42 | } 43 | return 0; 44 | } 45 | --------------------------------------------------------------------------------