Convert a vector to a string in C++
This post will discuss how to convert a vector to a string in C++.
1. Using String Constructor
If the vector is of type char, we can use the range constructor to construct a string object by copying the specified range of characters to it. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <vector> int main() { std::vector<char> vec { 'A', 'B', 'C' }; std::string str(vec.begin(), vec.end()); std::cout << str << std::endl; // ABC return 0; } |
2. Using String Stream
Another option to convert a std::vector to a std::string separated by a delimiter is using the string stream. The following C++ program demonstrates its usage using the iterator-based for loop:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <vector> #include <sstream> int main() { std::vector<int> vec { 6, 3, 8, -9, 1, -2, 8 }; std::stringstream ss; for (auto it = vec.begin(); it != vec.end(); it++) { if (it != vec.begin()) { ss << " "; } ss << *it; } std::cout << ss.str() << std::endl; // 6, 3, 8, -9, 1, -2, 8 return 0; } |
Here’s an equivalent version using the index-based for-loop:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <vector> #include <sstream> #include <iterator> int main() { std::vector<int> vec { 6, 3, 8, -9, 1, -2, 8 }; std::stringstream ss; for (size_t i = 0; i < vec.size(); i++) { if (i != 0) { ss << ", "; } ss << vec[i]; } std::cout << ss.str() << std::endl; // 6, 3, 8, -9, 1, -2, 8 return 0; } |
3. Using std::copy
Another alternative is to use the std::copy standard algorithm with the std::ostream_iterator class. It can be used as follows to get a comma-delimited string from a vector:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <vector> #include <sstream> #include <iterator> int main() { std::vector<int> vec { 6, 3, 8, -9, 1, -2, 8 }; std::ostringstream oss; if (!vec.empty()) { std::copy(vec.begin(), vec.end() - 1, std::ostream_iterator<int>(oss, ", ")); oss << vec.back(); } std::cout << oss.str() << std::endl; // 6, 3, 8, -9, 1, -2, 8 return 0; } |
4. Using std::accumulate
Another option to convert a vector to a string is using the standard function std::accumulate, defined in the header <numeric>. We can overwrite its default operation by providing a binary predicate to perform the concat operation on two strings and return the result.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> #include <vector> #include <numeric> template<typename T> std::string join(std::vector<T> const &vec, std::string delim) { if (vec.empty()) { return std::string(); } return std::accumulate(vec.begin() + 1, vec.end(), std::to_string(vec[0]), [](const std::string& a, T b){ return a + ", " + std::to_string(b); }); } int main() { std::vector<int> vec { 6, 3, 8, -9, 1, -2, 8 }; std::string delim = ", "; std::string s = join(vec, delim); std::cout << s << std::endl; // 6, 3, 8, -9, 1, -2, 8 return 0; } |
5. Using Boost
If your project is open to boost library, consider using the boost::algorithm::join algorithm. It joins all elements in the specified list into a string, where segments are concatenated by a given separator.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <vector> #include <boost/algorithm/string/join.hpp> #include <boost/range/adaptor/transformed.hpp> int main() { std::vector<int> vec { 6, 3, 8, -9, 1, -2, 8 }; std::string delim = ", "; auto to_string = static_cast<std::string(*)(int)>(std::to_string); // 6, 3, 8, -9, 1, -2, 8 std::cout << boost::algorithm::join(vec | boost::adaptors::transformed(to_string), delim); return 0; } |
That’s all about converting a vector to a 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 :)