Uploaded by Mu-Gamer

Assignment2-1

advertisement
#include<iostream>
#include <vector>
using namespace std;
class Vect
{
private:
vector<int>v_Ints;
vector<int>v_Ints2;
vector<int>v_Ints3;
public:
void Assign(int s,int v)
{
v_Ints.assign(s,v);
cout << "After assign the vector is: ";
for (unsigned int i = 0; i < v_Ints.size(); i++)
{
cout << v_Ints[i] << " ";
}
cout << endl;
}
void PushBack()
{
v_Ints.push_back(100);
int n = v_Ints.size();
cout << "The last element of the vector right now is: " << v_Ints[n - 1] << " and size:" << n << endl;
}
void PopBack()
{
v_Ints.pop_back();
int n = v_Ints.size();
cout << "After removing the size of vector is: " << n << " & last element is " << v_Ints[n - 1] << endl;
}
void Insert()
{
v_Ints.insert(v_Ints.begin(), 70);
cout << "After inserting at begining the front value is: " << v_Ints[0] << "\n";
}
void Erase()
{
v_Ints.erase(v_Ints.begin());
cout << "After erasing the begining value : " << v_Ints[0] << "\n";
}
void Swap()
{
int n = v_Ints.size();
cout << "Before swapping the fisrt value is " << v_Ints[0] << " last value: " << v_Ints[n - 1] << "\n";
swap(v_Ints[0], v_Ints[n - 1]);
cout << "After swapping the fisrt value is " << v_Ints[0] << " last value: " << v_Ints[n - 1] << "\n";
}
void Emplace()
{
v_Ints.emplace(v_Ints.begin(), 12);
v_Ints.emplace(v_Ints.begin(), 12);
cout << "After inserting at begining the front value is: " << v_Ints[0] << "\n";
}
void Emplace_Back()
{
v_Ints.emplace_back(60);
int n = v_Ints.size();
cout << "After inserting at back of the back value is: " << v_Ints[n - 1] << "\n";
}
void Clear()
{
v_Ints.clear();
int n = v_Ints.size();
cout << "After clearing the size of vector is: " << n << "\n";
}
void Iterators(int s)
{
int v;
cout << "Enter vector values:\n";
for (int j = 0; j < s; j++)
{
cin >> v;
v_Ints.push_back(v);
}
cout << endl;
}
void print1()
{
cout << "Output of all elements using begin() & end(): ";
for (auto i = v_Ints.begin(); i != v_Ints.end(); ++i)
cout << *i << " ";
cout << endl;
}
void print2()
{
cout << "Output of all elements using cbegin and cend: ";
for (auto i = v_Ints.cbegin(); i != v_Ints.cend(); ++i)
cout << *i << " ";
cout << endl;
}
void print3()
{
cout << "Output of all elements using crbegin and crend: ";
for (auto i = v_Ints.crbegin(); i != v_Ints.crend(); ++i)
cout << *i << " ";
cout << endl;
}
void print4()
{
cout << "Output of all elements using rbegin and rend: ";
for (auto i = v_Ints.rbegin(); i != v_Ints.rend(); ++i)
cout << *i << " ";
cout << endl << endl;
}
void Capacity(int s)
{
int v;
cout << "Enter vector values:\n";
for (int j = 0; j < s; j++)
{
cin >> v;
v_Ints2.push_back(v);
}
cout << endl;
}
void Size(int s)
{
cout << "Size of the vector is: " << v_Ints2.size() << "\n";
cout << "Max_Size of the vector is: " << v_Ints2.max_size() << endl;
cout << "Capacity of the vector is: " << v_Ints2.capacity() << endl;
v_Ints2.resize(s - 2);
cout << "After resize the Size of the vector is: " << v_Ints2.size() << endl;
cout << "Before reserve the size of the vector is: " << v_Ints2.size() << endl;
cout << "Before reserve the capacity of the vector is: " << v_Ints2.capacity() << endl;
v_Ints2.reserve(40);
cout << "After reserve the capacity of the vector is: " << v_Ints2.capacity() << endl;
cout << "After reserve the size of the vector is: " << v_Ints2.size() << endl;
}
void Shrink()
{
v_Ints2.shrink_to_fit();
cout << "\nVector elements are: ";
for (auto i = v_Ints2.begin(); i != v_Ints2.end(); i++)
cout << *i << " ";
}
void Empty()
{
v_Ints2.clear();
if (v_Ints2.empty() == true)
cout << "\nVector is empty" << endl;
else
cout << "\nVector is not empty" << endl;
cout << endl;
}
void Element_access(int s)
{
int v;
cout << "Enter vector values:\n";
for (int j = 0; j < s; j++)
{
cin >> v;
v_Ints3.push_back(v);
}
cout << "\nReference operator v3[2]= " << v_Ints3[2];
cout << "\nValue at position 3 is: " << v_Ints3.at(3);
cout << "\nFirst element of the vector is: " << v_Ints3.front();
cout << "\nLast element of the vector is: " << v_Ints3.back();
int* i = v_Ints3.data();
cout << "\nAll the elements of the vector is:\n";
for (int j = 0; j < v_Ints3.size(); j++)
{
cout << *i++ << " ";
}
cout << endl;
}
};
#include "Vector.h"
#include<iostream>
using namespace std;
int main()
{
int x;
int size;
cout << "Enter the size of vector :";
cin >> size;
cout << "Enter vector elements :";
cin >> x;
cout << endl;
Vect Vector , vector;
Vector.Assign(size,x);
Vector.PushBack();
Vector.PopBack();
Vector.Insert();
Vector.Erase();
Vector.Swap();
Vector.Emplace();
Vector.Emplace_Back();
Vector.Clear();
cout << endl;
vector.Iterators(size);
vector.print1();
vector.print2();
vector.print3();
vector.print4();
vector.Capacity(size);
vector.Size(size);
vector.Shrink();
vector.Empty();
vector.Element_access(size);
}
Download