In this article, we will discuss different ways to convert a vector of characters to a string in C++.
Table Of Contents
Method 1: Using range based for-loop
Create an empty string. Then, iterate over all the characters in vector, and append each character to the empty string created in the first step, using the push_back() function of string class. It accepts a character as argument, and adds that to the end of string. Let’s see an example,
#include <iostream> #include <vector> #include <string> int main() { // Vector of chars std::vector<char> vectorOfChars {'S', 'a', 'm', 'p', 'l', 'e', ' ', 'S', 't', 'r', 'i', 'n', 'g'}; // create an empty string std::string strValue = ""; // Iterate over all the characters in vector // and append each character to the string. for (const char& ch: vectorOfChars) { strValue.push_back(ch); } // print the string std::cout<< strValue<< std::endl; return 0; }
Output:
Sample String
It converted the vector of chars to a string.
Method 2: Using string class constructor
Consider a vector of characters as a range, and create two iterators pointing to the start, and end of the vector. Then pass these iterators to the constructor of string, while creating the string object. It will create a string from the vector of characters. Let’s see an example,
#include <iostream> #include <vector> #include <string> int main() { // Vector of chars std::vector<char> vectorOfChars {'S', 'a', 'm', 'p', 'l', 'e', ' ', 'S', 't', 'r', 'i', 'n', 'g'}; // Create a string from vector of chars std::string strValue(vectorOfChars.begin(), vectorOfChars.end()); // print the string std::cout<< strValue<< std::endl; return 0; }
Output:
Frequently Asked:
Sample String
It converted the vector of chars to a string.
Method 3: Using stringstream
Create an outputstream object. Then iterate over all the characters of vector, and add each character to the output stream. Once the loop ends, fetch the string from the output stream. This way we will create a string from the vector of characters. Let’s see an example,
#include <iostream> #include <vector> #include <string> #include <sstream> int main() { // Vector of chars std::vector<char> vectorOfChars {'S', 'a', 'm', 'p', 'l', 'e', ' ', 'S', 't', 'r', 'i', 'n', 'g'}; // Create a output stream std::ostringstream outputStream; // Iterate over all characters of vector, // and add that to the stream for (const char& ch: vectorOfChars) { outputStream << ch; } // Get the string from output stream. std::string strValue(outputStream.str()); // print the string std::cout<< strValue<< std::endl; return 0; }
Output:
Sample String
It converted the vector of chars to a string.
Method 4: using STL Algorithm transform()
The STL provides a function transform(), to perform an operation on a sequence. It accepts following arguments,
- Start, and end iterators of a sequence.
- A callback function as an operation, that needs to be applied over all the elements of sequence.
- An inserter iterator, in which all the values returned by callback operations will be stored.
So, to convert a vector of characters to a string, we can pass start, and end iterators of vector to the transform() function, along with a lambda function that accepts a character, and returns the same character. We will also pass the back_insert_iterator of string to the transform() function. It will populate the string with the characters from vector.
Let’s see an example,
#include <iostream> #include <vector> #include <string> #include <algorithm> int main() { // Vector of chars std::vector<char> vectorOfChars {'S', 'a', 'm', 'p', 'l', 'e', ' ', 'S', 't', 'r', 'i', 'n', 'g'}; // create an empty string std::string strValue = ""; // convert vector of chars to a string std::transform( vectorOfChars.begin(), vectorOfChars.end(), std::back_inserter(strValue), [](const char& ch) { return ch; }); // print the string std::cout<< strValue<< std::endl; return 0; }
Output:
Sample String
It converted the vector of chars to a string.
Method 5: Using string::append()
Iterate over all characters of a string, using a for loop. During iteration, add each character to an empty string using the append() function of string class. It takes two arguments i.e. the number of characters to be added, and the character to be added. Let’s see an example,
#include <iostream> #include <vector> #include <string> int main() { // Vector of chars std::vector<char> vectorOfChars {'S', 'a', 'm', 'p', 'l', 'e', ' ', 'S', 't', 'r', 'i', 'n', 'g'}; // create an empty string std::string strValue = ""; // convert vector of chars to a string for (const char& ch: vectorOfChars) { strValue.append(1, ch); } // print the string std::cout<< strValue<< std::endl; return 0; }
Output:
Pointers in C/C++ [Full Course]
Sample String
It converted the vector of chars to a string.
Summary
We learned about different ways to convert a vector of chars to a string in C++.