#include <iostream>
#include <vector>
#include <algorithm> // For std::sort
int main() {
// 1. Creation and Initialization
std::vector<int> numbers = {5, 2, 8, 1, 9};
// 2. Adding elements
numbers.push_back(4); // Adds 4 to the end
numbers.emplace_back(3); // Constructs 3 at the end (often more efficient)
// 3. Accessing elements
std::cout << "First element: " << numbers.front() << "\n";
std::cout << "Element at index 2: " << numbers[2] << "\n";
std::cout << "Element at index 3 (bound-checked): " << numbers.at(3) << "\n";
std::cout << "Last element: " << numbers.back() << "\n";
// 4. Modifying and Sorting
std::sort(numbers.begin(), numbers.end()); // Sorts the vector
// 5. Removing elements
numbers.pop_back(); // Removes the last element (3)
// 6. Iterating through the vector
std::cout << "\nSorted vector elements: ";
for (int num : numbers) {
std::cout << num << " ";
}
std::cout << "\n";
// 7. Size and Capacity
std::cout << "Current size: " << numbers.size() << "\n";
std::cout << "Is vector empty? " << (numbers.empty() ? "Yes" : "No") << "\n";
// 8. Clearing the vector
numbers.clear();
std::cout << "Size after clear: " << numbers.size() << "\n";
std::cout<<"======================"<<std::endl;
// 1. Initialize a vector with 5 names
std::vector<std::string> names = {"Charlie", "Alice", "Eve", "Bob", "Diana"};
// 2. Sort the names in ascending (alphabetical) order
std::sort(names.begin(), names.end());
// 3. Output the sorted names
std::cout << "Sorted names:\n";
for (const auto& name : names) {
std::cout << name << "\n";
}
return 0;
}