//
// main.cpp
// Vector (STL)
//
// Created by Himanshu on 23/09/21.
//
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void printIntVector (vector<int> l) {
vector<int>::iterator it;
for (it = l.begin(); it != l.end(); it++) {
cout<<(*it)<<" ";
}
cout<<endl;
}
void printFloatVector (vector<float> l) {
vector<float>::iterator it;
for (it = l.begin(); it != l.end(); it++) {
cout<<(*it)<<" ";
}
cout<<endl;
}
int main () {
// Declaration
vector<float> first, second;
// Initialisation
vector<int> vectorFirst({10, 20, 15});
// vectorSecond is intialised with 3 ints
// having value as 45
vector<int> vectorSecond(3, 45);
cout<<"vectorFirst elements:"<<endl;
printIntVector(vectorFirst);
cout<<"vectorSecond elements:"<<endl;
printIntVector(vectorSecond);
int arr[3] = {100, 101, 102};
vector<int> vectorThird;
vectorThird.insert(vectorThird.begin(), arr, arr+3);
cout<<"vectorThird elements after initialisation using arr:"<<endl;
printIntVector(vectorThird);
cout<<endl;
cout<<"vectorFirst first element: "<<vectorFirst.front()<<endl;
cout<<"vectorFirst last element: "<<vectorFirst.back()<<endl;
vector<int>::iterator it;
//Iterator to vector's first element
it = vectorFirst.begin();
//insert() returns pointer to the (new) first element
it = vectorFirst.insert (it, 2);
cout<<"vectorFirst elements after adding 2 at position (it):"<<endl;
printIntVector(vectorFirst);
//Iterator to 3rd element after vectorFirst.begin()
vectorFirst.insert (it+3, 2, 300);
cout<<"vectorFirst elements after adding 2 elements (value: 300) at position (it+3):"<<endl;
printIntVector(vectorFirst);
vectorFirst.swap(vectorSecond);
cout<<"vectorFirst elements after swap:"<<endl;
printIntVector(vectorFirst);
cout<<"vectorSecond elements after swap:"<<endl;
printIntVector(vectorSecond);
vectorFirst.clear();
cout<<"vectorFirst elements after clear:"<<endl;
printIntVector(vectorFirst);
if (vectorFirst.empty()) {
cout<<"vectorFirst is empty"<<endl;
}
// push_back – adds a new element
// at the end of the vector
first.push_back (1.0);
first.push_back (2.0);
first.push_back (3.0);
first.push_back (4.0);
first.push_back (5.0);
first.push_back (6.0);
cout<<"first vector elements after push_back():"<<endl;
printFloatVector(first);
cout<<"first vector elements after pop_back():"<<endl;
first.pop_back();
printFloatVector(first);
// erase – removes either a single element (position)
// or a range of elements ([first, last))
first.erase(first.begin() + 1, first.end() - 2);
cout<<"first vector elements after removing the elements from 2 to 4 [2..4)"<<endl;
printFloatVector(first);
return 0;
}