A good example for boost::algorithm::join

Dan Hook picture Dan Hook · Dec 2, 2009 · Viewed 71.1k times · Source

I recently wanted to use boost::algorithm::join but I couldn't find any usage examples and I didn't want to invest a lot of time learning the Boost Range library just to use this one function.

Can anyone provide a good example of how to use join on a container of strings? Thanks.

Answer

Tristram Gräbener picture Tristram Gräbener · Dec 2, 2009
#include <boost/algorithm/string/join.hpp>
#include <vector>
#include <iostream>

int main()
{
    std::vector<std::string> list;
    list.push_back("Hello");
    list.push_back("World!");

    std::string joined = boost::algorithm::join(list, ", ");
    std::cout << joined << std::endl;
}

Output:

Hello, World!