#include <iostream>
int main() {
// Array declaration and initialization
int numbers[5] = {1, 2, 3, 4, 5};
// Accessing array elements
std::cout << "First element: " << numbers[0] << std::endl;
std::cout << "Last element: " << numbers[4] << std::endl;
// Modifying array elements
numbers[2] = 10;
std::cout << "Modified element: " << numbers[2] << std::endl;
// Array traversal
for (int i = 0; i < 5; i++) {
std::cout << numbers[i] << " ";
}
std::cout << std::endl;
return 0;
}
#include <iostream>
int main() {
// 2D array declaration
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Accessing 2D array elements
std::cout << "Element at [1][1]: " << matrix[1][1] << std::endl;
// Printing 2D array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
#include <iostream>
#include <vector>
int main() {
// Vector declaration
std::vector<int> vec;
// Adding elements
vec.push_back(10);
vec.push_back(20);
vec.push_back(30);
// Accessing elements
std::cout << "Size: " << vec.size() << std::endl;
std::cout << "First element: " << vec[0] << std::endl;
// Vector iteration
for (int i = 0; i < vec.size(); i++) {
std::cout << vec[i] << " ";
}
std::cout << std::endl;
// Range-based for loop
for (int num : vec) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
#include <iostream>
int main() {
int value = 42;
int* ptr = &value; // Pointer to value
std::cout << "Value: " << value << std::endl;
std::cout << "Address of value: " << &value << std::endl;
std::cout << "Pointer value: " << ptr << std::endl;
std::cout << "Dereferenced pointer: " << *ptr << std::endl;
// Modifying value through pointer
*ptr = 100;
std::cout << "New value: " << value << std::endl;
// Pointer arithmetic
int numbers[] = {1, 2, 3, 4, 5};
int* numPtr = numbers;
std::cout << "First element: " << *numPtr << std::endl;
std::cout << "Second element: " << *(numPtr + 1) << std::endl;
return 0;
}
#include <iostream>
int main() {
// Dynamic allocation
int* dynamicArray = new int[5];
// Initialize array
for (int i = 0; i < 5; i++) {
dynamicArray[i] = i + 1;
}
// Use array
for (int i = 0; i < 5; i++) {
std::cout << dynamicArray[i] << " ";
}
std::cout << std::endl;
// Free memory
delete[] dynamicArray;
// Single dynamic variable
int* singleValue = new int(25);
std::cout << "Dynamic value: " << *singleValue << std::endl;
delete singleValue;
return 0;
}
#include <iostream>
#include <memory>
int main() {
// Unique pointer
std::unique_ptr<int> uniquePtr = std::make_unique<int>(42);
std::cout << "Unique pointer value: " << *uniquePtr << std::endl;
// Shared pointer
std::shared_ptr<int> sharedPtr1 = std::make_shared<int>(100);
std::shared_ptr<int> sharedPtr2 = sharedPtr1;
std::cout << "Shared pointer 1: " << *sharedPtr1 << std::endl;
std::cout << "Shared pointer 2: " << *sharedPtr2 << std::endl;
std::cout << "Reference count: " << sharedPtr1.use_count() << std::endl;
// Weak pointer
std::weak_ptr<int> weakPtr = sharedPtr1;
std::cout << "Weak pointer expired: " << weakPtr.expired() << std::endl;
return 0;
}