A Good Example for Boost::Algorithm::Join

A good example for boost::algorithm::join

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

Can boost:algorithm::join() concat a container of floats?

Sure, you can combine boost::algorithm::join and boost::adaptors::transformed to convert the doubles to strings and then join them together.

#include <iostream>
#include <vector>
#include <string>

#include <boost/algorithm/string/join.hpp>
#include <boost/range/adaptor/transformed.hpp>

int main()
{
using boost::adaptors::transformed;
using boost::algorithm::join;

std::vector<double> v{1.1, 2.2, 3.3, 4.4};

std::cout
<< join( v |
transformed( static_cast<std::string(*)(double)>(std::to_string) ),
", " );
}

Output:

1.100000, 2.200000, 3.300000, 4.400000


You can also use a lambda to avoid the ugly cast

join(v | transformed([](double d) { return std::to_string(d); }), ", ")

how to use boost::algorithm::join on tuples?

If you want to use join, you could wrap collection in boost::transform_iterator and add quotes if needed

Why in C++ function boost::algorithm::join_if a std::bad_cast exception is thrown?

boost::is_alpha is for characters

Use like following:-

cout << boost::algorithm::join_if(players, ", ",
[](const std::string & s){
return boost::all(s,boost::is_alpha());
}) << endl;

Here obviously, you won't get any output as space ' ' and numerals are present in players.

Use boost::alnum() instead.

Extracting and joining strings with boost::algorithms::join

With a little help from boost ranges you can have your cake and eat it.

It removes the sting from this mix: the complication is that you want to use an intermediate collection of std::string const& but, obviously that doesn't work with std::vector.

So, we drop the intermediate collection, instead using an adapted view (boost::adaptors::transformed) and, no need for a lambda either[¹], just use std::mem_fun: See it Live On Coliru

#include <boost/algorithm/string.hpp>
#include <boost/range/adaptors.hpp>

#include <vector>
#include <string>
#include <functional>

struct IPAddress {
std::vector<uint8_t> binaryValue;
std::string stringValue;
};

std::string foo(std::vector<IPAddress> const& addresses)
{
using namespace boost::adaptors;
return boost::algorithm::join(
addresses | transformed(std::mem_fn(&IPAddress::stringValue)),
";");
}

#include <iostream>

int main()
{
std::vector<IPAddress> const addresses {
{ {}, "test1" },
{ {}, "test2" },
};

std::cout << foo(addresses);
}

Prints

test1;test2

[¹] unless stringValue was an overloaded member function

Do I still need to use boost for algorithm::join?

There is no std::join in C++11 or C++14 so you will still have to use the boost version. There is an open proposal for it N3954

Accorging to http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/sd-1.htm The papper has been mailed out and is in the "Library Evolution" sub group.

Join a container of `std::string_view`

ITNOA

short C++20 answer version:

    using namespace std::literals;
const auto bits = { "https:"sv, "//"sv, "cppreference"sv, "."sv, "com"sv };
for (char const c : bits | std::views::join) std::cout << c;
std::cout << '\n';

since C++23 if you want to add special string or character between parts you can just use simple join_with and your code is just below (from official cppreference example)

#include <iostream>
#include <ranges>
#include <vector>
#include <string_view>

int main() {
using namespace std::literals;

std::vector v{"This"sv, "is"sv, "a"sv, "test."sv};
auto joined = v | std::views::join_with(' ');

for (auto c : joined) std::cout << c;
std::cout << '\n';
}

Note1: if you do not like use not stable release of language, you can simple use range-v3 library for join_with views

Note2: As Nicol Bolas you cannot join literally to exact one string_view without any copy (you can copy to string and ... :D), if you want to know more detailed about that you can see Why can't I construct a string_view from range iterators? SO question and answer.

Do I still need to use boost for algorithm::join?

There is no std::join in C++11 or C++14 so you will still have to use the boost version. There is an open proposal for it N3954

Accorging to http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/sd-1.htm The papper has been mailed out and is in the "Library Evolution" sub group.

How do I concatenate a vector of strings in c++ with boost?

Sure, boost provides a convenient algorithm for achieving what you are trying to do. In higher level languages you may have spotted a join function. Boost provides an equivalent algorithm in the join function.

#include <boost/algorithm/string/join.hpp>
using namespace std;

string data[] = {"abc","def","ghi"};
const size_t data_size = sizeof(data) / sizeof(data[0]);
vector<string> stringVector(data, data + data_size);
string joinedString = boost::algorithm::join(stringVector, "-");


Related Topics



Leave a reply



Submit