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

vector data structure and readme.md updated in c++ #790

Merged
merged 1 commit into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions C-Plus-Plus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ Format: -[Program name](name of the file)
- [Tim sort](Tim_Sort.cpp)
- [Twin prime](twin_prime.cpp)
- [Value of Pi by Random Numbers](ValueOfPi.cpp)
- [vector data structure](vector.cpp)
- [Word Break](Word-Break.cpp)
- [BFS with path](BFS.cpp)
- [Longest Path in DAG](Longest_path_in_DAG.cpp)
Expand Down
76 changes: 76 additions & 0 deletions C-Plus-Plus/vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/*Vectors are same as dynamic arrays with the ability to resize itself
automatically when an element is inserted or deleted*/
void iterate_over_vector(auto start,auto end)
{
for(auto i=start;i!=end;i++)
{
cout<<*i<<" ";
}
cout<<endl;
}
void insert_element(vector <int> &test,auto pos,int value)
{
// O(n+m)
test.insert(pos,value);
}
void erase_element(vector <int> &test,auto pos)
{
// O(n)
test.erase(pos);
}

bool is_vector_empty(vector <int> &test)
{
return test.empty();
}

int main()
{
vector <int> test;
test.push_back(5); // add 5 at the end O(1)
test.push_back(4); // added 4 after 5 O(1)
test.push_back(8); // Vector elements are placed in contiguous storage.
test.push_back(1); // O(1)
/* Iterate over vector
begin() � Returns an iterator pointing to the first element in the vector
end() � Returns an iterator pointing to the theoretical element that follows the last element in the vector */

cout<<"Initial vector: ";
iterate_over_vector(test.begin(),test.end());
cout<<"Size of vector: "<<test.size()<<endl;

insert_element(test,test.begin()+2,5); //insert element at given iterator
cout<<"vector after insertion: ";
iterate_over_vector(test.begin(),test.end());
cout<<"Size of vector: "<<test.size()<<endl;
test.pop_back(); //delete last element
erase_element(test,test.begin()+1); //erase element at given iterator

cout<<"vector after erasing some elements: ";
iterate_over_vector(test.begin(),test.end());
cout<<"Size of vector: "<<test.size()<<endl;
cout<<"Is vector empty: "<<is_vector_empty(test)<<endl;

test.clear(); // remove all the elements of the vector container

cout<<"Is vector empty: "<<is_vector_empty(test)<<endl;

return 0;
}

/*
output:

Initial vector: 5 4 8 1
Size of vector: 4
vector after insertion: 5 4 5 8 1
Size of vector: 5
vector after erasing some elements: 5 5 8
Size of vector: 3
Is vector empty: 0
Is vector empty: 1
*/