When Arrays Are Not Enough: std::array and std::vector
Modern C++ offers two important standard library containers—std::array and std::vector—that address many limitations of raw arrays. While raw arrays are fixed in size and lack many safety features, std::array and std::vector provide improved usability, type safety, and integration with modern C++ features. Both are included in the <array> and <vector> headers, respectively. std::array is a wrapper around fixed-size arrays, adding member functions and better compatibility with STL algorithms. std::vector, on the other hand, is a dynamic array that can grow or shrink at runtime, making it much more flexible for situations where the number of elements isn't known in advance. Both containers make your code safer and easier to maintain than using raw arrays.
main.cpp
123456789101112131415161718192021#include <iostream> #include <array> #include <vector> int main() { // Using std::array (fixed size) std::array<int, 5> arr = {1, 2, 3, 4, 5}; std::cout << "std::array values: "; for (const auto& val : arr) std::cout << val << ' '; std::cout << "\nSize of std::array: " << arr.size() << std::endl; // Using std::vector (dynamic size) std::vector<int> vec = {10, 20, 30}; vec.push_back(40); // Add an element std::cout << "std::vector values: "; for (const auto& val : vec) std::cout << val << ' '; std::cout << "\nSize of std::vector: " << vec.size() << std::endl; }
In the code above, you see two modern alternatives to raw arrays in action. Declaring a std::array requires specifying both the element type and the size as template parameters, such as std::array<int, 5>. Initialization is similar to a raw array but wrapped in curly braces. You can access its size at any time using the .size() member function, which is not available for raw arrays. For dynamic needs, std::vector allows you to declare a container without specifying its size up front—just the element type. You can add elements at runtime using functions like .push_back(), and retrieve the current size with .size(). This flexibility and the availability of useful member functions make both containers more robust and expressive than plain arrays.
main.cpp
1234567891011121314151617181920212223#include <iostream> #include <vector> int main() { std::vector<std::string> names; // Add names dynamically names.push_back("Alice"); names.push_back("Bob"); names.push_back("Charlie"); std::cout << "There are " << names.size() << " names:\n"; for (const auto& name : names) { std::cout << name << std::endl; } // Add another name names.push_back("Diana"); std::cout << "Now there are " << names.size() << " names:\n"; for (const auto& name : names) std::cout << name << std::endl; }
You should use std::array when you need a fixed-size collection of elements with the safety and convenience of STL containers. It is ideal for situations where the size is known at compile time and does not change. In contrast, std::vector is the right choice when you need a collection that can change size during execution, such as when reading an unknown number of inputs or building a list that grows as the program runs. Raw arrays are rarely needed in modern C++ code, except for compatibility with legacy APIs or when maximum performance is critical and the size is always fixed.
std::vector can grow and shrink dynamically, unlike raw arrays and std::array, whose sizes are fixed at compile time.
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Can you give examples of when to use std::array versus std::vector?
What are some common mistakes to avoid when using these containers?
How do std::array and std::vector compare in terms of performance?
Fantastisk!
Completion rate forbedret til 7.69
When Arrays Are Not Enough: std::array and std::vector
Stryg for at vise menuen
Modern C++ offers two important standard library containers—std::array and std::vector—that address many limitations of raw arrays. While raw arrays are fixed in size and lack many safety features, std::array and std::vector provide improved usability, type safety, and integration with modern C++ features. Both are included in the <array> and <vector> headers, respectively. std::array is a wrapper around fixed-size arrays, adding member functions and better compatibility with STL algorithms. std::vector, on the other hand, is a dynamic array that can grow or shrink at runtime, making it much more flexible for situations where the number of elements isn't known in advance. Both containers make your code safer and easier to maintain than using raw arrays.
main.cpp
123456789101112131415161718192021#include <iostream> #include <array> #include <vector> int main() { // Using std::array (fixed size) std::array<int, 5> arr = {1, 2, 3, 4, 5}; std::cout << "std::array values: "; for (const auto& val : arr) std::cout << val << ' '; std::cout << "\nSize of std::array: " << arr.size() << std::endl; // Using std::vector (dynamic size) std::vector<int> vec = {10, 20, 30}; vec.push_back(40); // Add an element std::cout << "std::vector values: "; for (const auto& val : vec) std::cout << val << ' '; std::cout << "\nSize of std::vector: " << vec.size() << std::endl; }
In the code above, you see two modern alternatives to raw arrays in action. Declaring a std::array requires specifying both the element type and the size as template parameters, such as std::array<int, 5>. Initialization is similar to a raw array but wrapped in curly braces. You can access its size at any time using the .size() member function, which is not available for raw arrays. For dynamic needs, std::vector allows you to declare a container without specifying its size up front—just the element type. You can add elements at runtime using functions like .push_back(), and retrieve the current size with .size(). This flexibility and the availability of useful member functions make both containers more robust and expressive than plain arrays.
main.cpp
1234567891011121314151617181920212223#include <iostream> #include <vector> int main() { std::vector<std::string> names; // Add names dynamically names.push_back("Alice"); names.push_back("Bob"); names.push_back("Charlie"); std::cout << "There are " << names.size() << " names:\n"; for (const auto& name : names) { std::cout << name << std::endl; } // Add another name names.push_back("Diana"); std::cout << "Now there are " << names.size() << " names:\n"; for (const auto& name : names) std::cout << name << std::endl; }
You should use std::array when you need a fixed-size collection of elements with the safety and convenience of STL containers. It is ideal for situations where the size is known at compile time and does not change. In contrast, std::vector is the right choice when you need a collection that can change size during execution, such as when reading an unknown number of inputs or building a list that grows as the program runs. Raw arrays are rarely needed in modern C++ code, except for compatibility with legacy APIs or when maximum performance is critical and the size is always fixed.
std::vector can grow and shrink dynamically, unlike raw arrays and std::array, whose sizes are fixed at compile time.
Tak for dine kommentarer!