Characters in an array of chars, ending with the nul character, \0 is a string. The array above actually contains the phrases, “I love you.” and “anything else” separated by the character, ‘\0’.
would print:
ignoring anything else. This is the traditional way of having a string in C++. Anything else should be ignored after the ‘\0’ character if the array content is to be considered a string.
With the pointer, the above string would be coded as:
and
would print:
An array of characters is a constant pointer to characters, ending with ‘\0’. This explains why const is used in the statement, “const char* ptrStr = “I love you.”;”. The double quotes eliminate the use of the array literal structure and ‘\0’.
With the string class, a string object for the above phrase would be
and
would print:
The string object could still have been instantiated as,
The indirect question is how to convert an array string literal to a literal double quote and how to convert an array literal or double quote literal to a string object. The direct question is, when these values are elements of the vector, how to do these conversions. This article explains that.
Before getting into the core explanation, remember that 'E' is a character, while "E" is a string. In order to use vectors of string objects, the program should begin with:
#include <string>
#include <vector>
using namespace std;
Article Content
- Introduction – see above
- Vector of Array Chars To Vector Pointer Chars
- Vector of Pointer to Chars To Vector of String Objects
- Conclusion
Vector of Array Chars To Vector of Pointer Chars
This section explains how to convert a vector of character arrays that form strings to a vector of constant-pointer-to-strings.
Now,
and
mean the same thing inside, as the following code shows:
for (int i=0; arrStr[i] != '\0'; i++)
cout << arrStr[i];
cout << endl;
const char* ptrStr = "I love you.";
for (int i=0; ptrStr[i] != '\0'; i++)
cout << ptrStr[i];
cout << endl;
The output is:
I love you
All code segments for this article are in the main() function body. For the array, the array name with [i] is used to read all the values in the array. The pointer name with [i] is used to read all the values in the string literal for the pointer. Note that ‘\0’ is implicit at the end of the literal string. What is constant for both cases is the pointer and not the value. An array name is a constant pointer to the sequence of characters, which should end with ‘\0’.
So, a vector of arrays of chars, where each array ends with ‘\0’ or a vector of double-quote string literals, should be declared in the same way, as follows:
Consider the following vector of fruit names, where each fruit name is an array of chars, ending with ‘\0’.
char fruit2[] = {'s','t','r','a','w','b','e','r','r','y','\0'};
char fruit3[] = {'p','a','s','s','i','o','n',' ','f','r','u','i','t','\0'};
char fruit4[] = {'b','a','n','a','n','a','\0'};
char fruit5[] = {'o','r','a','n','g','e','\0'};
vector<const char*> vtr {fruit1, fruit2, fruit3, fruit4, fruit5};
The vector of fruits has been constructed by writing the array names as elements in the vector. This same vector can be constructed with string literals as follows:
So, there is no need to convert a vector of arrays-of-chars to a vector of const-pointers-to-chars. They are the same thing, underneath. Since they are the same thing, an array string value can be read into a const-pointer-to-chars, as the following code shows:
char fruit2[] = {'s','t','r','a','w','b','e','r','r','y','\0'};
char fruit3[] = {'p','a','s','s','i','o','n',' ','f','r','u','i','t','\0'};
char fruit4[] = {'b','a','n','a','n','a','\0'};
char fruit5[] = {'o','r','a','n','g','e','\0'};
vector<const char*> vtr {fruit1, fruit2, fruit3, fruit4, fruit5};
for (int i=0; i<vtr.size(); i++) {
const char* str = vtr[i];
cout << str << ", ";
}
cout << endl;
The output is:
The line,
is where the supposed conversion takes place.
Vector of Pointer to Chars To Vector of String Objects
The question of converting a vector of pointer-to-chars to vector-of-string-objects, is the same as the question of converting a vector of arrays-of-chars to vector-of-string-objects. Consider the following statement:
vtr = {"papaya", "strawberry", "passion fruit", "banana", "orange"};
The following declaration has the above declaration, in string object form:
vtr = {string("papaya"), string("strawberry"), string("passion fruit"), string("banana"), string("orange")};
In this case, “#include ” has to be at the top of the program. Notice the template argument and the string object values.
Now, it is possible to assign a string literal, to become the content of a string object, as the following three code segments show:
const char* strLit = "abc";
string str = strLit;
char arr[] = {'a','b','c','\0'};
string str = arr;
With this knowledge, each string literal can be read into a string object variable, as the following code shows:
for (int i=0; i<vtr.size(); i++) {
string str = vtr[i];
cout << str << ", ";
}
cout << endl;
The output is:
The line that does the conversion from literal to string object is:
If the vector values were array strings, then the following code will do the same thing:
char fruit2[] = {'s','t','r','a','w','b','e','r','r','y','\0'};
char fruit3[] = {'p','a','s','s','i','o','n',' ','f','r','u','i','t','\0'};
char fruit4[] = {'b','a','n','a','n','a','\0'};
char fruit5[] = {'o','r','a','n','g','e','\0'};
vector<const char*> vtr {fruit1, fruit2, fruit3, fruit4, fruit5};
for (int i=0; i<vtr.size(); i++) {
string str = vtr[i];
cout << str << ", ";
}
cout << endl;
The output is the same:
The line that does the conversion from literal to a string object is still the same:
Vector of String Literals to Vector of String Objects
To really change a vector of string literals to a vector of string objects, the following procedure will have to be followed:
- Create another empty vector, but this time, a vector of string objects.
- Copy each string literal from the vector of const-character-pointers, to the vector of string objects, by pushing.
- Destroy the old vector of literals.
The following code illustrates this:
vector<string> vtrNew;
for (int i=0; i<vtr.size(); i++) {
vtrNew.push_back(vtr[i]);
}
vtr.~vector();
for (int i=0; i<vtrNew.size(); i++) {
string str = vtrNew[i];
cout << str << ", ";
}
cout << endl;
The output is:
The line for the code that destroys the old vector is:
The content of the vector is destroyed, but not the vector name. However, the old vector name cannot be reused (in the same scope).
Conclusion
An array string literal and a constant pointer to character sequence literal are the same things underneath. An array string literal is an array literal of chars, ending with ‘\0’. A const-pointer-to-char-sequence literal sequence of consecutive characters delimited by double quotes, e.g., “abc”. ‘\0’ is implicit at the end of the const-pointer-to-char-sequence literal.
An array string literal or a const-pointer-to-char-sequence literal can be assigned to the identifier of a const-pointer-to-char-sequence, as illustrated in the following code segment:
const char* ss = "def";
ss = arr;
const char* sss = ss;
There is no really need to convert a vector of array strings to a vector of string literals in double-quotes. It suffices to read each array string of the vector to the identifier of a const-pointer-to-char-sequence.
However, if a vector of string objects is really required from a vector of string literals, then the conversion should be done as follows:
- Create another empty vector, but this time, a vector of string objects.
- Copy each string literal from the vector of const character pointers to the vector of string objects by pushing.
- Destroy the old vector of literals.
Destroying a vector means destroying its content (elements), but not the name of the vector.