Convert a vector of chars to std::string in C++
This post will explore how to convert a vector of chars to std::string in C++.
1. Using Range Constructor
The idea is to use a string constructor that can accept input iterators to an initial and final position of the vector, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <vector> int main() { std::vector<char> input({ 'a', 'b', 'c' }); std::string s(input.begin(), input.end()); std::cout << s; return 0; } |
Output:
abc
2. Using std::ostringstream function
Another good alternative is to use the std::ostringstream included in <sstream> header file. The idea is to insert all characters into the stream and then write the contents of its buffer to the std::string using its str() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <vector> #include <sstream> int main() { std::vector<char> input({ 'a', 'b', 'c' }); std::ostringstream out; for (char c: input) { out << c; } std::string s(out.str()); std::cout << s; return 0; } |
Output:
abc
3. Using std::transform function
We can also use the standard algorithm std::transform, which applies an operation to elements of the specified range and stores the result in another range, which begins at the specified output iterator. The operation may be a unary operation function, a lambda expression, or an object of a class implementing the () operator. We need to include the <algorithm> header file for this.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<char> input({ 'a', 'b', 'c' }); std::string s; std::transform(input.begin(), input.end(), std::back_inserter(s), [](char c) { return c; }); std::cout << s; return 0; } |
Output:
abc
4. Using For loop
The idea is to call the push_back() function for each input character. push_back appends the specified character at the end of the string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <vector> int main() { std::vector<char> input({ 'a', 'b', 'c' }); std::string s; for (char c: input) { s.push_back(c); } std::cout << s; return 0; } |
Output:
abc
We can also use the +=operator replacing push_back, which is overloaded for chars.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <vector> int main() { std::vector<char> input({ 'a', 'b', 'c' }); std::string s; for (char c: input) { s += c; } std::cout << s; return 0; } |
Output:
abc
Another good alternative is to call the append() function that appends the specified number of characters at the end of the string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <vector> int main() { std::vector<char> input({ 'a', 'b', 'c' }); std::string s; for (char c: input) { s.append(1, c); } std::cout << s; return 0; } |
Output:
abc
Finally, we also have the insert(pos, n, c) function that inserts n copies of character c beginning at character pos.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <vector> int main() { std::vector<char> input({ 'a', 'b', 'c' }); std::string s; for (unsigned i = 0; i < input.size(); i++) { s.insert(i, 1, input[i]); } std::cout << s; return 0; } |
Output:
abc
That’s all about converting a vector of chars to std::string in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)