c++ - How to implode a vector of strings into a string (the elegant way)

C++ - How to implode a vector of strings into a string (the elegant way)

In C++, you can "implode" a std::vector<std::string> into a single std::string by joining the elements of the vector with a specified delimiter. The elegant way to achieve this is to use the Standard Library algorithms and facilities, such as std::accumulate from the <numeric> header or using modern C++ features such as the range-based for loop and std::ostringstream.

Here are a few approaches to accomplish this:

Using std::accumulate

You can use std::accumulate from the <numeric> header to concatenate the elements with a delimiter:

#include <iostream> #include <vector> #include <string> #include <numeric> std::string implode(const std::vector<std::string>& vec, const std::string& delimiter) { return std::accumulate(vec.begin(), vec.end(), std::string(), [&delimiter](const std::string& a, const std::string& b) { return a.empty() ? b : a + delimiter + b; }); } int main() { std::vector<std::string> words = {"Hello", "World", "C++"}; std::string result = implode(words, ", "); std::cout << result << std::endl; // Output: Hello, World, C++ return 0; } 

Using std::ostringstream

You can use std::ostringstream from the <sstream> header for a more manual but flexible approach:

#include <iostream> #include <vector> #include <string> #include <sstream> std::string implode(const std::vector<std::string>& vec, const std::string& delimiter) { std::ostringstream oss; for (size_t i = 0; i < vec.size(); ++i) { if (i != 0) { oss << delimiter; } oss << vec[i]; } return oss.str(); } int main() { std::vector<std::string> words = {"Hello", "World", "C++"}; std::string result = implode(words, ", "); std::cout << result << std::endl; // Output: Hello, World, C++ return 0; } 

Using C++20 std::ranges (if available)

If you are using C++20 or later, you can use std::ranges to make the code more concise:

#include <iostream> #include <vector> #include <string> #include <ranges> #include <algorithm> #include <sstream> std::string implode(const std::vector<std::string>& vec, const std::string& delimiter) { std::ostringstream oss; auto it = vec.begin(); if (it != vec.end()) { oss << *it++; } std::ranges::for_each(it, [&oss, &delimiter](const std::string& s) { oss << delimiter << s; }); return oss.str(); } int main() { std::vector<std::string> words = {"Hello", "World", "C++"}; std::string result = implode(words, ", "); std::cout << result << std::endl; // Output: Hello, World, C++ return 0; } 

Key Points

  • Elegance: std::accumulate is a functional approach that avoids explicit loops.
  • Flexibility: std::ostringstream provides more control and is useful for custom formatting.
  • Modern C++: If using C++20, std::ranges offers a concise and expressive way to handle such operations.

Choose the method that best fits your needs and the C++ version you are using.

Examples

  1. How to join elements of a vector into a single string using std::accumulate in C++?

    Description: Use std::accumulate to concatenate elements of a vector into a single string with a separator.

    Code:

    #include <iostream> #include <vector> #include <numeric> int main() { std::vector<std::string> vec = {"apple", "banana", "cherry"}; std::string result = std::accumulate(vec.begin(), vec.end(), std::string(), [](const std::string &a, const std::string &b) { return a.empty() ? b : a + "," + b; }); std::cout << result << std::endl; return 0; } 
  2. How to concatenate vector of strings using std::stringstream in C++?

    Description: Use std::stringstream to concatenate vector elements efficiently.

    Code:

    #include <iostream> #include <vector> #include <sstream> int main() { std::vector<std::string> vec = {"apple", "banana", "cherry"}; std::stringstream ss; for (size_t i = 0; i < vec.size(); ++i) { if (i != 0) ss << ","; ss << vec[i]; } std::string result = ss.str(); std::cout << result << std::endl; return 0; } 
  3. How to join vector of strings using std::copy and std::ostream_iterator in C++?

    Description: Use std::copy and std::ostream_iterator to join elements with a delimiter.

    Code:

    #include <iostream> #include <vector> #include <iterator> #include <sstream> int main() { std::vector<std::string> vec = {"apple", "banana", "cherry"}; std::ostringstream oss; std::copy(vec.begin(), vec.end() - 1, std::ostream_iterator<std::string>(oss, ",")); oss << vec.back(); std::string result = oss.str(); std::cout << result << std::endl; return 0; } 
  4. How to use boost::algorithm::join to concatenate vector elements in C++?

    Description: Use the Boost library's boost::algorithm::join to concatenate elements with a delimiter.

    Code:

    #include <iostream> #include <vector> #include <boost/algorithm/string/join.hpp> int main() { std::vector<std::string> vec = {"apple", "banana", "cherry"}; std::string result = boost::algorithm::join(vec, ","); std::cout << result << std::endl; return 0; } 
  5. How to join elements of a vector using a custom function in C++?

    Description: Implement a custom join function to concatenate vector elements.

    Code:

    #include <iostream> #include <vector> std::string join(const std::vector<std::string> &vec, const std::string &delimiter) { std::string result; for (size_t i = 0; i < vec.size(); ++i) { result += vec[i]; if (i != vec.size() - 1) result += delimiter; } return result; } int main() { std::vector<std::string> vec = {"apple", "banana", "cherry"}; std::string result = join(vec, ","); std::cout << result << std::endl; return 0; } 
  6. How to concatenate vector elements using std::for_each in C++?

    Description: Use std::for_each with a lambda to concatenate elements.

    Code:

    #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<std::string> vec = {"apple", "banana", "cherry"}; std::string result; std::for_each(vec.begin(), vec.end(), [&result](const std::string &s) { if (!result.empty()) result += ","; result += s; }); std::cout << result << std::endl; return 0; } 
  7. How to implode a vector of strings using std::transform_reduce in C++20?

    Description: Use std::transform_reduce to concatenate vector elements in C++20.

    Code:

    #include <iostream> #include <vector> #include <numeric> #include <string> int main() { std::vector<std::string> vec = {"apple", "banana", "cherry"}; std::string result = std::transform_reduce( vec.begin() + 1, vec.end(), vec[0], std::plus<>(), [](const std::string &a, const std::string &b) { return a + "," + b; } ); std::cout << result << std::endl; return 0; } 
  8. How to join vector elements using std::accumulate with an initializer in C++?

    Description: Use std::accumulate with an initializer list to join vector elements.

    Code:

    #include <iostream> #include <vector> #include <numeric> int main() { std::vector<std::string> vec = {"apple", "banana", "cherry"}; std::string result = std::accumulate( vec.begin(), vec.end(), std::string(), [](const std::string &a, const std::string &b) { return a.empty() ? b : a + "," + b; }); std::cout << result << std::endl; return 0; } 
  9. How to concatenate vector elements using a range-based for loop in C++?

    Description: Use a range-based for loop to concatenate elements with a delimiter.

    Code:

    #include <iostream> #include <vector> int main() { std::vector<std::string> vec = {"apple", "banana", "cherry"}; std::string result; for (const auto &s : vec) { if (!result.empty()) result += ","; result += s; } std::cout << result << std::endl; return 0; } 
  10. How to use std::transform with std::back_inserter to concatenate vector elements in C++?

    Description: Use std::transform with std::back_inserter to concatenate vector elements.

    Code:

    #include <iostream> #include <vector> #include <algorithm> #include <iterator> int main() { std::vector<std::string> vec = {"apple", "banana", "cherry"}; std::string result; std::transform( vec.begin(), vec.end(), std::back_inserter(result), [&result](const std::string &s) { if (!result.empty()) result += ","; return s; }); std::cout << result << std::endl; return 0; } 

More Tags

asynchronous android-broadcast remote-desktop sparkling-water width gruntjs entity-relationship partition citations vscode-extensions

More Programming Questions

More Everyday Utility Calculators

More Tax and Salary Calculators

More Other animals Calculators

More Housing Building Calculators