Yes, it is possible to create an array of strings in C++ that have different sizes or share similar functionality. Here's an example:
#include <iostream>
#include <string>
int main() {
std::string strings[] = {"hello", "world", "!", "C++ is", "fun"};
// Printing the length of each string in the array
for (auto str : strings) {
std::cout << str << " has length " << str.length() << std::endl;
}
return 0;
}
In this example, we have created an array of strings with different lengths and functionality (e.g., some strings are punctuation marks while others are complete sentences). We can access each string in the array using its index (e.g., strings[0]
would give us the string "hello"). We can also use string member functions (e.g., length()
) on each element of the array to perform operations specific to that string.
Asked: 2023-02-03 11:00:00 +0000
Seen: 8 times
Last updated: Nov 27 '21