. Advertisement .
..3..
. Advertisement .
..4..
How to create a string array in C++ is a common question for any beginner in the field of programming. There are various types of data contained in the C++ programming language. When coming to the type of string data, it refers to an array of characters ending with “\0” which is a terminating character. There are several ways to declare an array of strings. Keep on reading if you want to figure them out!
What is the definition of a string array?
Before jumping into how to create a string array in C++, you must acquire a deep understanding of the definition of a string array. In the most simple clarification, a string array is a simple array that includes various strings.
In the programming language of C++, the string is performed as a characters array or utilizing a class of strings supported by C++. The terminating character to end each string or array element is a null one. The use of a character array to represent strings is taken from the “C” language in a direct way, due to C does not contain any string type.
How to create a string array in C++
In this article, you would be presented about 4 typical ways to create a string array in C++ including:
- Using the std::vector Container.
- Using the std::array Container.
- Using the string arr [ ] Notation.
- Using the char arr [ ] [ ] Notation.
If you are in need of finding a solution to create a string array, do not skip any discussion in the next section below.
Using the std::vector data structure
The STL contains std::vector, which can offer the common data types a solution to create a strings array through a dynamic array. You can access the std::vector effectively with no cost due to its elements being stored contiguously.
However, due to the former requirements of maximizing memory management efficiency, the std::vector is allocated extra space and takes over larger storage than the other static arrays.
Moreover, there are many beneficial member functions provided by the std::vector aiming to manipulate elements such as using the function of emplace_back to construct in-place elements. You can refer to the demonstration below.
#include<iostream>
#include<string>
#include<vector>
using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;
template<typename T>
void printVector(std::vector<T> v) {
for (const auto &item : v) {
cout << item << "; ";
}
cout << endl;
}
int main() {
vector<string> str_arr1 = { "Sunny Cove", "Willow Cove",
"Golden Cove", "Ocean Cove"};
printVector(str_arr1);
str_arr1.emplace_back("Palm Cove");
printVector(str_arr1);
return EXIT_SUCCESS;
}
Output:
Sunny Cove; Willow Cove; Golden Cove; Ocean Cove;
Sunny Cove; Willow Cove; Golden Cove; Ocean Cove; Palm Cove;
Using the std::array data structure
Another way to resolve how to create a string array in C++ is using the std::array data structure for the static array declaration. These arrays reach equal efficiency in utilizing memory as the C-arrays and support the accessibility of the generic member functions.
The aggregate initialization is used by the std::array data structure. Besides, the common iterator can be applied and manipulated by using the algorithm of std::copy. The code demonstration would be performed as an example for you below.
#include<iostream>
#include<string>
#include<array>
#include<iterator>
using std::cout; using std::cin;
using std::endl; using std::string;
using std::array; using std::copy;
int main() {
array<string, 4> str_arr2 = { "Sunny Cove", "Willow Cove",
"Golden Cove", "Ocean Cove" };
array str_arr3 = { "Sunny Cove", "Willow Cove",
"Golden Cove", "Ocean Cove" };
std::copy(str_arr3.begin(), str_arr3.end(),
std::ostream_iterator<string>(cout,"; "));
cout << endl;
return EXIT_SUCCESS;
}
Output:
Sunny Cove; Willow Cove; Golden Cove; Ocean Cove;
Using the string arr [ ] Notation
Alternatively, you can use the string objects array in the C-style to create a string array. In this way, an array of a fixed-element string would be declared. This type of array can utilize index notation for random accessibility and a range-based for loop for iteration. To manipulate and access custom functions contained in the class of the std::array, you need to conduct custom functions.
#include<iostream>
#include<string>
using std::cout; using std::cin;
using std::endl; using std::string;
int main() {
string str_arr4[4] = { "Sunny Cove", "Willow Cove",
"Golden Cove", "Ocean Cove" };
for (auto &i : str_arr4) {
cout << i << "; ";
}
cout << endl;
return EXIT_SUCCESS;
}
Output:
Sunny Cove; Willow Cove; Golden Cove; Ocean Cove;
Using the char arr [ ] [ ] Notation
An additional method to create a string array in C++ is to allocate a fixed-length array of strings, each of which has the predefined maximum character numbers. Remember that this way would declare an array of a two-dimension char and store each string within every single row of the matrix.
In case there are more characters in the string contained in the initializer list than the second dimension of the array, the last string part would be cut off to ensure the line fit.
#include<iostream>
#include<string>
using std::cout; using std::cin;
using std::endl; using std::string;
int main() {
char str_arr5[4][20] = { "Sunny Cove", "Willow Cove",
"Golden Cove", "Ocean Cove" };
for (auto &i : str_arr5) {
cout << i << "; ";
}
cout << endl;
return EXIT_SUCCESS;
}
Output:
Sunny Cove; Willow Cove; Golden Cove; Ocean Cove;
Final Words
This article has guided you over the overview of string array and the main 4 methods of how to create a string array in C++. Hope that those tutorials demonstrated above would be helpful for any readers who are struggling with manipulating functions to create an array of strings. By the way, you can switch to another post on the website if you still want to learn more about programming.
Leave a comment