A good example for boost::algorithm::join

C++StringBoostJoin

C++ Problem Overview


I recently wanted to use http://www.boost.org/doc/libs/1_41_0/doc/html/string_algo/reference.html#header.boost.algorithm.string.join_hpp"> 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.

C++ Solutions


Solution 1 - C++

#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!

Solution 2 - C++

std::vector<std::string> MyStrings;
MyStrings.push_back("Hello");
MyStrings.push_back("World");
std::string result = boost::algorithm::join(MyStrings, ",");

std::cout << result; // prints "Hello,World"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDan HookView Question on Stackoverflow
Solution 1 - C++Tristram GräbenerView Answer on Stackoverflow
Solution 2 - C++KeatsPeeksView Answer on Stackoverflow