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;
}
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;
}
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;
}
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;
}
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.
Similar Reads
Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i
7 min read
List of Vectors in C++ STL In C++, the list of vector refers to the list container in which each element is a vector. In this article, we will learn about the list of vectors in C++.Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main() { list<vector<int>> l = {{1, 3}, {2
3 min read
Array of Vectors in C++ STL Prerequisite: Arrays in C++, Vector in C++ STL An array is a collection of items stored at contiguous memory locations. It is to store multiple items of the same type together. This makes it easier to get access to the elements stored in it by the position of each element. Vectors are known as dynam
3 min read
Strings in C++ In C++, strings are sequences of characters that are used to store words and text. They are also used to store data, such as numbers and other types of information in the form of text. Strings are provided by <string> header file in the form of std::string class.Creating a StringBefore using s
5 min read
C++ Vector of Pointers Prerequisites Pointers in C++Vectors in C++ Vector of pointers are vectors that can hold multiple pointers. Each pointer within a vector of pointers points to an address storing a value. We can use the vector of pointers to manage values that are not stored in continuous memory. How to Create Vector
6 min read
Array of Strings in C++ In C++, a string is sequence of characters that is used to store textual information. Internally, it is implemented as a dynamic array of characters. Array of strings is the array in which each element is a string.We can easily create an array of string in C++ as shown in the below example:C++#inclu
4 min read