Implode a vector of strings into a comma-separated string in C++
This post will discuss how to implode a vector of strings into a comma-separated string in C++.
1. Using std::copy function
An elegant solution is to use std::copy to copy the vector’s contents to the string stream using the output iterator. The following solution demonstrates this using std::ostream_iterator, which takes a delimiter on construction and writes it to the stream after inserting each element. This solution works but leaves a trailing delimiter.
|
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 |
#include <iostream> #include <vector> #include <algorithm> #include <sstream> #include <iterator> std::string join(std::vector<std::string> const &strings, std::string delim) { std::stringstream ss; std::copy(strings.begin(), strings.end(), std::ostream_iterator<std::string>(ss, delim.c_str())); return ss.str(); } int main() { std::vector<std::string> strings = {"A", "B", "C", "D"}; std::string delim = ", "; std::string str = join(strings, delim); std::cout << str << std::endl; // A, B, C, D, return 0; } |
2. Using std::accumulate
Another option is to use the std::accumulate provided by the standard library. It is defined in the header file <numeric>. To leave no trailing delimiter, perform the concat operation on two strings starting from the second element, as shown below:
|
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 29 30 31 |
#include <iostream> #include <vector> #include <algorithm> #include <sstream> #include <iterator> #include <numeric> std::string join(std::vector<std::string> const &strings, std::string delim) { if (strings.empty()) { return std::string(); } return std::accumulate(strings.begin() + 1, strings.end(), strings[0], [&delim](std::string x, std::string y) { return x + delim + y; } ); } int main() { std::vector<std::string> strings = {"A", "B", "C", "D"}; std::string delim = ", "; std::string str = join(strings, delim); std::cout << str << std::endl; // A, B, C, D return 0; } |
The behavior of this example is equivalent to:
|
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 |
#include <iostream> #include <vector> #include <algorithm> #include <sstream> #include <iterator> std::string join(std::vector<std::string> &strings, std::string delim) { return std::accumulate(strings.begin(), strings.end(), std::string(), [&delim](std::string &x, std::string &y) { return x.empty() ? y : x + delim + y; }); } int main() { std::vector<std::string> strings = {"A", "B", "C", "D"}; std::string delim = ", "; std::string s = join(strings, delim); std::cout << s << std::endl; // A, B, C, D return 0; } |
3. Using Boost
Another good alternative is to use boost’s boost::algorithm::join function, which is defined in the header file <boost/algorithm/string/join.hpp>. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <vector> #include <boost/algorithm/string/join.hpp> int main() { std::vector<std::string> strings = {"A", "B", "C", "D"}; std::string delim = ", "; std::string s = boost::algorithm::join(strings, delim); std::cout << s << std::endl; // A, B, C, D return 0; } |
That’s all about imploding a vector of strings into a comma-separated 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 :)