How to use the Array Class In C++
An array is a homogeneous mixture of data that is stored continuously in the memory space. The STL container array can be used to allocate a fixed-size array. It may be used very similarly to a vector, but the size is always fixed.
Example:
C++
// C++ program to demonstrate // array of string using STL array #include <array> #include <iostream> #include <string> // Driver code int main() { // Initialize array std::array<std::string, 4> colour{ "Blue" , "Red" , "Orange" , "Yellow" }; // Printing Strings stored in array for ( int i = 0; i < 4; i++) std::cout << colour[i] << "\n" ; return 0; } |
Blue Red Orange Yellow
These are by no means the only ways to make a collection of strings. C++ offers several container classes, each of which has various tradeoffs and features, and all of them exist to fill requirements that you will have in your projects. Explore and have fun!
Conclusion: Out of all the methods, Vector seems to be the best way for creating an array of Strings in C++.
s.
Array of Strings in C++ – 5 Different Ways to Create
In C++, a string is usually just an array of (or a reference/points to) characters that ends with the NULL character ‘\0‘. A string is a 1-dimensional array of characters and an array of strings is a 2-dimensional array of characters where each row contains some string.
Below are the 5 different ways to create an Array of Strings in C++:
- Using Pointers
- Using 2-D Array
- Using the String Class
- Using the Vector Class
- Using the Array Class
Contact Us