How to Create a Vector of Strings in C++

How to create a vector of strings in C++? If the list of strings is short, then a very quick way to create the vector is as in the following program:
#include <iostream>

#include <string>

#include <vector>

using namespace std;

{
        vectorvtr = {"donkey", "goat", "cat", "pig", "dog", "Cow", "Sheep", "horse", "water buffalo", "red fox"};
        return 0;
    }

The program begins with the inclusion of the iostream library, which is needed for keyboard input, and output to the terminal (screen). This is followed by the inclusion of the string library, which is needed for automatic composing of strings. That is followed by the inclusion of the vector library, which makes the creation and use of vectors easy. After that is a statement, which insists that any name not preceded by std, is from the standard namespace. Then, there is the C++ main function in the code. In order to have a vector of strings, all the above code heading is necessary.

The first statement in the main() function is the creation of the vector, vtr, of animal names. It begins with the reserved word, vector. Immediately after this are angle brackets, which have the word, “string”. Here, “string” is a template parameter specialization. If it was a vector of characters, then “char” would have been in the place of “string”; if it was a vector of integers, then “int” would have been in the place of “string”; if it was a vector of floats, then “float” would have been in the place of “string”; and so on.

After the angle brackets, there is a space, and then the name of the vector chosen by the programmer. This is followed by space, assignment, space again, end then the initializer_list. The initializer_list has the names (string literals) of the animals; each name is in quotes. If there is space between a pair of quotes, then that space becomes part of the corresponding string. Initializer_list is delimited by braces.

This statement is a definition. Without the assignment operator and the initializer_list, the statement would be just a declaration. With the initializer_list, the statement becomes a definition as well as it is still a declaration.

“How to create a vector of strings in C++” also means, “What are the different ways of creating a vector of strings, in C++?” The different ways of creating a vector of strings in C++ are quite many. The most commonly used ways are illustrated in this article.

Beginning with Empty Vector of Strings

An empty vector can be created first, before the string elements are added. When an element is added into a vector, the element is said to be pushed_back into the vector, because the element is inserted at the back. The following C++ main function shows how this can be done:

int main()
    {
        vectorvtr;
vtr.push_back("donkey"); vtr.push_back("goat"); vtr.push_back("cat"); vtr.push_back("pig");
vtr.push_back("dog"); vtr.push_back("Cow"); vtr.push_back("Sheep"); vtr.push_back("horse");
vtr.push_back("water buffalo"); vtr.push_back("red fox");
        return 0;
    }

The first statement creates an empty vector of strings. Each of the other statements pushes_back a string into the vector. To achieve this, begin with the vector name, then the dot, and then the push_back() member function. The argument for the push_back function is either the string literal, or the identifier (variable) for the string literal.

Creating with Initializer_list

One way of creating with initializer_list, is as shown previously. The other way is as follows:

int main()
    {
        vectorvtr({"donkey", "goat", "cat", "pig", "dog", "Cow", "Sheep", "horse", "water buffalo", "red fox"});
        return 0;
    }

The expression,

vector<string> vtr(arguments)

is an example of a constructor. This time, the initializer_list is in the parentheses of the constructor. There is no assignment operator in the construction (creation) of the vector of strings.

Creating with Initializer_list Identifier

In the above two examples with initializer_list, the initializer_list literal was used. Instead of using the literal, the identifier of the initializer_list can also be used. In the following code, the identifier of a vector (initializer_list) is assign to the new vector with the assignment operator:

int main()
    {
        vectoroldVector = {"donkey", "goat", "cat", "pig", "dog", "Cow", "Sheep", "horse", "water buffalo", "red fox"};
        vectornewVector = oldVector;
        return 0;
    }

The last-but-one statement is the key-statement here.

The following code shows how the identifier of a vector (initializer_list) is used in the parentheses of a new vector constructor:

int main()
    {
        vectoroldVector = {"donkey", "goat", "cat", "pig", "dog", "Cow", "Sheep", "horse", "water buffalo", "red fox"};
        vectornewVector(oldVector);
        return 0;
    }

The identifier can also be that of a rvalue reference, as shown in the following code:

int main()
    {
        vector&&oldVector = {"donkey", "goat", "cat", "pig", "dog", "Cow", "Sheep", "horse", "water buffalo", "red fox"};
        vectornewVector(oldVector);
        return 0;
    }

Note the use and position of &&.

assign() Member Function

The vector class has a member function which is assign(). The argument is an initializer_list (literal). So, after creating an empty vector, the assign() member function can be used to put in the first elements of the list, as the following code shows:

    int main()
    {
        vectorvtr;
vtr.assign({"donkey", "goat", "cat", "pig", "dog", "Cow", "Sheep", "horse", "water buffalo", "red fox"});
        return 0;
    }

When the Number of Elements is Known

When the number of strings is known, then it can be put in the parentheses of the constructor. If the number of strings is 10, for example, then the vector will be constructed with 10 default staring values. The default string value is the empty string, “”. After that, the different practical strings can be inserted, at their appropriate positions, using the square brackets (subscript) operator. See the following code:

int main()
    {
        vectorvtr(10);
vtr[0] = "donkey"; vtr[1] = "goat"; vtr[2] = "cat"; vtr[3] = "pig";
vtr[4] = "dog"; vtr[5] = "cow"; vtr[6] = "sheep"; vtr[7] = "horse";
vtr[8] = "water buffalo"; vtr[9] = "red fox";
        return 0;
    }

Default Vector String Value

The default string value is the empty string, “”, which has no space and no character. An empty vector of strings, does not have any string value. This means it also does not have any empty default string value. On the other hand, a vector created with a number of default strings has that number of default strings before practical values (strings) can be added. The following code demonstrates that any default vector string is the “”:

int main()

{

vector<string> vtr(10);

if (vtr[4] == "")

cout << "seen" << endl;

return 0;

}

The output is “seen”, confirming that any default vector string is, “”.

Conclusion

A vector of strings is created the way a vector of any other type would be created. Remember to make the template specialization, string. Do not forget to include the string library and the vector library. The common ways of creating vectors with string as the element type have been illustrated above.



from https://ift.tt/KaWTLl8

Post a Comment

0 Comments