Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vector data structure in c++ stl #910

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions C++ STL/vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
#include <vector>
using namespace std;

int main()
{
// Assign vector
vector<int> 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;
}