std::arraystd::array is a container that encapsulates fixed-size arrays. It provides a standard interface for accessing and manipulating elements, making it a versatile tool in the C++ Standard Library (STL). Unlike traditional C-style arrays, std::array offers type safety, bounds checking, and compatibility with STL algorithms.
std::arrayat() provide safe access to elements with bounds checking.#include <array>
#include <iostream>
int main() {
// Declare a std::array of 5 integers, initialized to zero
std::array<int, 5> arr1;
// Initialize with specific values
std::array<int, 3> arr2 = {1, 2, 3};
// Using the array constructor
std::array<double, 4> arr3{4.0, 5.5, 6.7, 8.9};
return 0;
}
#include <iostream>
#include <array>
int main() {
std::array<int, 5> arr = {10, 20, 30, 40, 50};
// Using subscript operator []
std::cout << "Element at index 2: " << arr[2] << std::endl;
// Using at() method with bounds checking
try {
std::cout << "Element at index 10: " << arr.at(10) << std::endl;
} catch (const std::out_of_range& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}
#include <iostream>
#include <array>
int main() {
std::array<int, 5> arr = {10, 20, 30, 40, 50};
// Using range-based for loop
for (const auto& elem : arr) {
std::cout << elem << " ";
}
std::cout << std::endl;
// Using iterators
for (auto it = arr.begin(); it != arr.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <array>
int main() {
std::array<int, 5> arr = {10, 20, 30, 40, 50};
// Get the size of the array
std::cout << "Size: " << arr.size() << std::endl;
// Check if the array is empty (always false for non-zero-sized arrays)
std::cout << "Empty: " << arr.empty() << std::endl;
return 0;
}
#include <iostream>
#include <array>
int main() {
std::array<int, 5> arr = {10, 20, 30, 40, 50};
// Front element
std::cout << "Front: " << arr.front() << std::endl;
// Back element
std::cout << "Back: " << arr.back() << std::endl;
return 0;
}
#include <iostream>
#include <array>
int main() {
std::array<int, 5> arr = {10, 20, 30, 40, 50};
// Modify elements using subscript operator []
arr[2] = 35;
// Modify elements using at() method
arr.at(3) = 45;
for (const auto& elem : arr) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <array>
int main() {
std::array<int, 5> arr;
// Fill all elements with a specific value
arr.fill(100);
for (const auto& elem : arr) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <array>
int main() {
std::array<int, 3> arr1 = {1, 2, 3};
std::array<int, 3> arr2 = {4, 5, 6};
// Lexicographical comparison
if (arr1 < arr2) {
std::cout << "arr1 is less than arr2" << std::endl;
} else {
std::cout << "arr1 is not less than arr2" << std::endl;
}
return 0;
}
#include <iostream>
#include <array>
#include <algorithm>
int main() {
std::array<int, 5> arr = {34, 7, 23, 32, 5};
// Sort the array in ascending order
std::sort(arr.begin(), arr.end());
for (const auto& elem : arr) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
std::array Over C-style Arrays: std::array provides better type safety and compatibility with STL algorithms.std::array to avoid undefined behavior.at() for safer element access, especially in production code.std::array is a powerful and flexible container that should be part of every C++ developer's toolkit. Its fixed size, type safety, and compatibility with STL algorithms make it an excellent choice for scenarios where you need a simple, efficient array-like structure. By following best practices and leveraging its features, you can write safer, more maintainable code.
This comprehensive guide provides a detailed overview of std::array, covering its declaration, initialization, element access, iteration, and various methods. By understanding these concepts, you can effectively use std::array in your C++ projects to write clean, efficient, and safe code.