Open In App

Vector of Strings in C++

Last Updated : 23 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, a vector of strings is a std::vector container that stores multiple strings. It is useful when you need to store a collection of string data in a single container and refer to them quickly. In this article, we will learn about the vector of strings and how to create and use it in C++.

Vector of Strings in C++

A vector of strings is a std::vector that stores elements of type std::string. In C++, a vector can be created by specifying std::string as the template parameter when declaring the vector.

Syntax

vector<string> v;

where v is the name of the vector.

Example

C++
// C++ Program to demonstrate a vector of strings
#include <bits/stdc++.h>
using namespace std;

int main() {
  
    // Creating a vector of strings
    vector<string> v;

    // Insert strings in vector
    v.push_back("Hi");
    v.push_back("Geeks,");
    v.push_back("Welcome!");

    for(auto i : v)
        cout << i << " ";
    return 0;
}

Output
Hi Geeks, Welcome! 

Vector of Strings with STL Algorithms

As string is fully supported data structure in C++ just like integers, we can perform any operation of vector of string that we can perform on vector of any other primitive data type such as ints, chars, etc.

Also, STL algorithms fully support the vector of strings. For example, std::sort() will sort it in the default order, while the std::accumulate will give you the contatenated string containing all the strings of the vector.

More Examples of Vector of Strings

The below examples demonstrate the behaviour of vector of strings with various STL algorithms.

Example 1: Sorting a Vector of Strings

C++
// C++ Program to sort a vector of strings
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<string> v = {"Hi", "Geeks,",
                        "Welcome!"};

    // Sorting vector of strings
    sort(v.begin(), v.end());

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
Geeks, Hi Welcome! 

Example 2: Reversing Whole Vector and Each String

C++
// C++ Program to reverse whole vector
// of strings
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<string> v = {"Hi", "Geeks,",
                        "Welcome!"};

    // Reverse whole vector of strings
    reverse(v.begin(), v.end());
  	
  	// Reverse each string
  	for (auto& i: v) {
      reverse(i.begin(), i.end());
    }

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
!emocleW ,skeeG iH 

Example 3: Concatenating All Strings Using std::accumulate()

C++
// C++ Program to concatenate all strings
// in vector
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<string> v = {"Hi", "Geeks,",
                        "Welcome!"};

    // Concatenating all strings
    string res = accumulate(v.begin(),
                    v.end(), string(""));

    cout << res;
    return 0;
}

Output
HiGeeks,Welcome!

Array of Strings and Vector of Strings in C++

Array of strings were popularly used in C for efficiently storing a collection of strings in a program. Although we can also use it in C++, it is recommended to use the vector of strings due to the following reasons:

  • Vector of strings provide dynamic size while array size cannot be changed once declared.
  • One of the main strenth of vector of strings is the ful support STL algorithms like sort(), reverse(), accumulate(), and others. Array may not support all the algorithms.
  • Array of strings are difficult to maintain and use while most of the complex thing for vector of strings is handled automatically.
  • Array of strings are the array of pointers which is notorious for being risky to use due to no error checking and loopholes.

Next Article
Article Tags :
Practice Tags :

Similar Reads