From 9e68f5329ffcfba7474ff27220be80bb5323994e Mon Sep 17 00:00:00 2001 From: Prakash Mondal <97402824+rko0211@users.noreply.github.com> Date: Wed, 4 Oct 2023 01:29:14 +0530 Subject: [PATCH] Add vector datastructure in c++ stl --- C++ STL/vector.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 C++ STL/vector.cpp diff --git a/C++ STL/vector.cpp b/C++ STL/vector.cpp new file mode 100644 index 00000000..6f450bf6 --- /dev/null +++ b/C++ STL/vector.cpp @@ -0,0 +1,44 @@ +#include +#include +using namespace std; + +int main() +{ + // Assign vector + vector vect; + + // fill the vector with 5 ten times + vect.assign(10, 5); + + // Get the size of the vector + int vector_size = vect.size(); + + // Iterate the vector using for loop + cout << "Vector elements are\n"; + for (int i = 0; i < vector_size; i++) + { + cout << vect[i] << " "; + } + // inserts 20 to the last position + vect.push_back(20); + vector_size = vect.size(); + cout << "\nThe last element after insert new element is: " << vect[vector_size - 1]; + + // removes last element + vect.pop_back(); + // Print the vector + cout << "\nThe vector elements after removing the last element are: "; + for (int i = 0; i < vect.size(); i++) + { + cout << vect[i] << " "; + } + + // inserts 8 at the beginning + vect.insert(vect.begin(), 8); + cout << "\nThe first element is: " << vect[0]; + + // removes the first element + vect.erase(vect.begin()); + cout << "\nThe first element is: " << vect[0]; + return 0; +} \ No newline at end of file