Joins a range of elements and returns the result as a std::string.

Synopsis

Declared in <absl/strings/str_join.h>

template<
    typename Iterator,
    typename Formatter>
std::string
StrJoin(
    Iterator start,
    Iterator end,
    std::string_view sep,
    Formatter&& fmt);

Description

absl::StrJoin() takes a range, a separator string to use between the elements joined, and an optional Formatter responsible for converting each argument in the range to a string.

If omitted, the default AlphaNumFormatter() is called on the elements to be joined.

Example 1: // Joins a collection of strings. This pattern also works with a collection // of absl::string_view or even const char*. std::vector<std::string> v = {"foo", "bar", "baz"}; std::string s = absl::StrJoin(v, "‐"); EXPECT_EQ(s, "foo‐bar‐baz");

Example 2: // Joins the values in the given std::initializer_list<> specified using // brace initialization. This pattern also works with an initializer_list // of ints or absl::string_view ‐‐ any AlphaNum‐compatible type. std::string s = absl::StrJoin({"foo", "bar", "baz"}, "‐"); EXPECT_EQs, "foo‐bar‐baz");

Example 3: // Joins a collection of ints. This pattern also works with floats, // doubles, int64s ‐‐ any StrCat()‐compatible type. std::vector<int> v = {1, 2, 3, ‐4}; std::string s = absl::StrJoin(v, "‐"); EXPECT_EQ(s, "1‐2‐3‐‐4");

Example 4: // Joins a collection of pointer‐to‐int. By default, pointers are // dereferenced and the pointee is formatted using the default format for // that type; such dereferencing occurs for all levels of indirection, so // this pattern works just as well for std::vector<int**> as for // std::vector<int*>. int x = 1, y = 2, z = 3; std::vector<int*> v = {&x, &y, &z}; std::string s = absl::StrJoin(v, "‐"); EXPECT_EQ(s, "1‐2‐3");

Example 5: // Dereferencing of std::unique_ptr<> is also supported: std::vector<std::unique_ptr<int>> v v.emplace_back(new int(1)); v.emplace_back(new int(2)); v.emplace_back(new int(3)); std::string s = absl::StrJoin(v, "‐"); EXPECT_EQ(s, "1‐2‐3");

Example 6: // Joins a std::map, with each key‐value pair separated by an equals // sign. This pattern would also work with, say, a // std::vector<std::pair<>>. std::map<std::string, int> m = { {"a", 1}, {"b", 2}, {"c", 3}}; std::string s = absl::StrJoin(m, ",", absl::PairFormatter("=")); EXPECT_EQ(s, "a=1,b=2,c=3");

Example 7: // These examples show how absl::StrJoin() handles a few common edge // cases: std::vector<std::string> v_empty; EXPECT_EQ(absl::StrJoin(v_empty, "‐"), "");

std::vector<std::string> v_one_item = {"foo"}; EXPECT_EQ(absl::StrJoin(v_one_item, "‐"), "foo");

std::vector<std::string> v_empty_string = {""}; EXPECT_EQ(absl::StrJoin(v_empty_string, "‐"), "");

std::vector<std::string> v_one_item_empty_string = {"a", ""}; EXPECT_EQ(absl::StrJoin(v_one_item_empty_string, "‐"), "a‐");

std::vector<std::string> v_two_empty_string = {"", ""}; EXPECT_EQ(absl::StrJoin(v_two_empty_string, "‐"), "‐");

Example 8: // Joins a std::tuple<T...> of heterogeneous types, converting each to // a std::string using the absl::AlphaNum class. std::string s = absl::StrJoin(std::make_tuple(123, "abc", 0.456), "‐"); EXPECT_EQ(s, "123‐abc‐0.456");

Return Value

The joined string.

Parameters

Name

Description

start

Iterator to the first element to join.

end

Iterator past the last element to join.

sep

The separator placed between joined elements.

fmt

The formatter used to convert each element to a string.

Created with MrDocs