ARRAYS
#include<array>
std::array<int, 4> arr; // Declaration of std::array with type 'int' and size '4'
size_t size = arr.size(); // Get the size of the array
bool isEmpty = arr.empty(); // Check if the array is empty or not
int first_Element = arr.front(); // Get the first element of the array
int last_Element = arr.back(); // Get the last element of the array
VECTOR
Different ways to initialize a vector:
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main() {
// 1. Initializing by pushing values one by one:
vector<int> vect; // Create an empty vector
vect.push_back(10); // Pushing value 10
vect.push_back(20); // Pushing value 20
vect.push_back(30); // Pushing value 30
// Output: 10 20 30
// 2. Specifying size and initializing all values:
int n = 3;
vector<int> vect(n, 10);
// Create a vector of size n with all values as 10
// Output: 10 10 10
// 3. Initializing like arrays:
vector<int> vect{ 10, 20, 30 };
// Initialize a vector like an array
// Output: 10 20 30
// 4. Initializing from an array:
int arr[] = { 10, 20, 30 };
int n = sizeof(arr) / sizeof(arr[0]);
vector<int> vect(arr, arr + n);
// Initialize a vector from an array
// Output: 10 20 30
// 5. Initializing from another vector:
vector<int> vect1{ 10, 20, 30 };
vector<int> vect2(vect1.begin(), vect1.end());
// Initialize a vector from another vector
// Output: 10 20 30
// 6. Initializing all elements with a particular value:
vector<int> vect1(10); // Create a vector with size 10
int value = 5;
fill(vect1.begin(), vect1.end(), value);
// Initialize all elements with value
// Output: 5 5 5 5 5 5 5 5 5 5
// 7. Initialize an array with consecutive numbers using std::iota:
vector<int> vec(5); // Declare a vector with size 5
iota(vec.begin(), vec.end(), 1); // Initialize with consecutive numbers
// Output: 1 2 3 4 5
return 0;
}
Using iterators for different operations:
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Iterators
vector<int> g1;
// Adding elements to the vector
for (int i = 1; i <= 5; i++)
g1.push_back(i);
// Output of begin and end:
cout << "Output of begin and end: ";
for (auto i = g1.begin(); i != g1.end(); ++i)
cout << *i << " ";
// Output: 1 2 3 4 5
cout << endl << endl;
// Output of cbegin and cend:
cout << "Output of cbegin and cend: ";
for (auto i = g1.cbegin(); i != g1.cend(); ++i)
cout << *i << " ";
// Output: 1 2 3 4 5
cout << endl << endl;
// Output of rbegin and rend:
cout << "Output of rbegin and rend: ";
for (auto ir = g1.rbegin(); ir != g1.rend(); ++ir)
cout << *ir << " ";
// Output: 5 4 3 2 1
cout << endl << endl;
// Output of crbegin and crend:
cout << "Output of crbegin and crend: ";
for (auto ir = g1.crbegin(); ir != g1.crend(); ++ir)
cout << *ir << " ";
// Output: 5 4 3 2 1
return 0;
}
Capacity and size functions for vectors:
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Capacity
vector<int> g1;
// Adding elements to the vector
for (int i = 1; i <= 5; i++)
g1.push_back(i);
cout << "Capacity:\n";
cout << "Size: " << g1.size();
// Output: Size: 5
cout << "\nCapacity: " << g1.capacity();
// Output: Capacity: 8
cout << "\nMax Size: " << g1.max_size();
// Output: Max Size: 4611686018427387903
cout << endl << endl;
// Resizing the vector size
g1.resize(4);
cout << "Size after resize: " << g1.size();
// Output: Size after resize: 4
cout << endl;
// Checking if the vector is empty or not
if (g1.empty() == false)
cout << "Vector is not empty";
else
cout << "Vector is empty";
// Output: Vector is not empty
cout << endl;
// Shrink the vector to fit its size
g1.shrink_to_fit();
cout << "Vector elements are: ";
for (auto it = g1.begin(); it != g1.end(); it++)
cout << *it << " ";
// Output: Vector elements are: 1 2 3 4
cout << endl << endl;
// Element access
cout << "Element Access:\n";
cout << "Reference operator [g]: g1[2] = " << g1[2];
// Output: Reference operator [g]: g1[2] = 3
cout << "\nat